473,405 Members | 2,379 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Trigger and DML INSERT on separate table

I have a trigger. Each time the triggering event fires, I want to
insert some information into another 'audit-like' table (not at all
related to the trigger). Some psuedo-code is below as to what I've
been working with; I am unable to get the INSERT to work?
Recommendations; what am I doing wrong?
-------------------
CREATE OR REPLACE TRIGGER test
AFTER INSERT OR DELETE OR UPDATE ON testdb
FOR EACH ROW
DECLARE
mytestvar number;

test_job number;
BEGIN
IF mytestvar > 0 THEN
RAISE_APPLICATION_ERROR(-20001, 'Problem here. . .');
dbms_job.submit(test_job,'insert into MYTABLE (COL1, COL2)
values ('THIS WAS', 'AN ERROR'); commit;', NULL);
dbms_job.run(test_job, false);
END IF;
END;
/

Thanks.
Jul 19 '05 #1
3 6911

"Tom Urbanowicz" <ts*@landacorp.com> wrote in message
news:6d*************************@posting.google.co m...
I have a trigger. Each time the triggering event fires, I want to
insert some information into another 'audit-like' table (not at all
related to the trigger). Some psuedo-code is below as to what I've
been working with; I am unable to get the INSERT to work?
Recommendations; what am I doing wrong?
-------------------
CREATE OR REPLACE TRIGGER test
AFTER INSERT OR DELETE OR UPDATE ON testdb
FOR EACH ROW
DECLARE
mytestvar number;

test_job number;
BEGIN
IF mytestvar > 0 THEN
RAISE_APPLICATION_ERROR(-20001, 'Problem here. . .');
dbms_job.submit(test_job,'insert into MYTABLE (COL1, COL2)
values ('THIS WAS', 'AN ERROR'); commit;', NULL);
dbms_job.run(test_job, false);
END IF;
END;
/

Thanks.


You said that you are unable to get the insert to work. You did NOT say
what error you are getting. Your inference to 'not at all related' implies
to me you are getting a mutating tables error. If so then it's likely that
you have foreign keys or some ogther constraint (which will act as a read)
between the table with the trigger and the audit table that the trigger is
hitting.

If it's somethign else then you'd best give us all more details.

HTH Alan
Jul 19 '05 #2

Originally posted by Tom Urbanowicz
I have a trigger. Each time the triggering event fires, I want to
insert some information into another 'audit-like' table (not at all
related to the trigger). Some psuedo-code is below as to what I've
been working with; I am unable to get the INSERT to work?
Recommendations; what am I doing wrong?
-------------------
CREATE OR REPLACE TRIGGER test
AFTER INSERT OR DELETE OR UPDATE ON testdb
FOR EACH ROW
DECLARE
mytestvar number;

test_job number;
BEGIN
IF mytestvar > 0 THEN
RAISE_APPLICATION_ERROR(-20001, 'Problem here. . .');
dbms_job.submit(test_job,'insert into MYTABLE (COL1, COL2)
values ('THIS WAS', 'AN ERROR'); commit;', NULL);
dbms_job.run(test_job, false);
END IF;
END;
/

Thanks.

You are testing whether mytestvar > 0 but you never gave it a value, so
it will not be.

Also, once you have called RAISE_APPLICATION_ERROR, the trigger is
aborted (exception raised), so it will never get to the
dbms_job.submit line.

I guess you are trying to use dbms_job to overcome the fact that if
the triggering statement rolls back, so would any insert into
mytable. That isn't the right approach. Instead, you should use
PRAGMA AUTONOMOUS_TRANSACTION to commit the insert into mytable
regardless of whether the main transaction is committed or rolled
back, perhaps like this:

CREATE OR REPLACE PROCEDURE log_error
( p_error_text1 IN VARCHAR2
, p_error_text2 IN VARCHAR2
)
IS
PRAGMA AUTONOMOUS_TRANSACTION;
BEGIN
INSERT INTO mytable (col1, col2)
VALUES (p_error_text1, p_error_text2);
COMMIT;
END;
/
CREATE OR REPLACE TRIGGER test
AFTER INSERT OR DELETE OR UPDATE ON testdb
FOR EACH ROW
DECLARE
mytestvar number := 99;
BEGIN
IF mytestvar > 0 THEN
log_error( 'THIS WAS', 'AN ERROR');
RAISE_APPLICATION_ERROR(-20001, 'Problem here. . .');
END IF;
END;
/

--
Posted via http://dbforums.com
Jul 19 '05 #3
Thanks very much for the help; works as needed!
-Tom

andrewst <me*********@dbforums.com> wrote in message news:<30****************@dbforums.com>...
Originally posted by Tom Urbanowicz
I have a trigger. Each time the triggering event fires, I want to
insert some information into another 'audit-like' table (not at all
related to the trigger). Some psuedo-code is below as to what I've
been working with; I am unable to get the INSERT to work?
Recommendations; what am I doing wrong?

Jul 19 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Trevor Fairchild | last post by:
I am trying to create a very minimal auditing system for a series of databases. I am in the process of writing Update triggers for 5 Tablse. I will write a trigger for each table-the trigger's...
6
by: Scott CM | last post by:
I have a multi-part question regarding trigger performance. First of all, is there performance gain from issuing the following within a trigger: SELECT PrimaryKeyColumn FROM INSERTED opposed...
2
by: Jules Alberts | last post by:
Hello everyone, Several columns in sereval tables in my DB should always be lowercase. I now have a simple function: create or replace function code_lower() returns trigger as ' begin...
0
by: JohnO | last post by:
Thanks to Serge and MarkB for recent tips and suggestions. Ive rolled together a few stored procedures to assist with creating audit triggers automagically. Hope someone finds this as useful as...
5
by: Bob Stearns | last post by:
I have two (actually many) dates in a table I want to validate on insertion. The following works in the case of only one WHEN clause but fails with two (or more), with the (improper?...
12
by: Zvonko | last post by:
Hi! I have a interesting problem. I need to write a trigger that wil go off after every sixth row is inserted/updated. CREATE TABLE . ( varchar(13) COLLATE Croatian_CI_AS NOT NULL, ...
2
by: tolcis | last post by:
I have a trigger that should be execute on each row insert and only if appcode = 'I' and datasent = 0. It should execute a DTS package. The DTS package by itself runs about 6 seconds. Trigger...
0
by: Bit of a dummy | last post by:
Hi I have a problem with a trigger. The trigger is used to calculate and store a total of minutes worked in a week. When the timeWorked or shiftConfirmedOrCancelled columns are updated the...
3
by: Tom Urbanowicz | last post by:
I have a trigger. Each time the triggering event fires, I want to insert some information into another 'audit-like' table (not at all related to the trigger). Some psuedo-code is below as to what...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.