Home » SQL & PL/SQL » SQL & PL/SQL » Getting duplicate entry in audit table after triggered (Oracle DB 10g)
Getting duplicate entry in audit table after triggered [message #616881] Sun, 22 June 2014 02:56 Go to next message
aruntutor
Messages: 10
Registered: June 2014
Location: chennai
Junior Member
Hi All,

Its making duplicate entry in a table after a trigger was fired. Given the details below , please chk and lme know the solution for my better understanding .


CREATE OR REPLACE TRIGGER referencing_clause
AFTER UPDATE
ON person
REFERENCING NEW AS NEW OLD AS OLD
FOR EACH ROW
BEGIN
INSERT INTO audit_log
(o_fname, o_lname, n_fname, n_lname, chng_by, chng_when)
VALUES
(:OLD.fname, :OLD.lname, :NEW.fname, :NEW.lname, USER,SYSDATE);
END referencing_clause;
/

INSERT INTO person (fname, lname) VALUES ('Dan', 'Morgan');

SELECT * FROM person;
SELECT * FROM audit_log;

UPDATE person
SET lname = 'Dangerous';

SELECT * FROM person;
SELECT * FROM audit_log;....here am getting duplicate entry.
Dan Morgan Dan Dangerous ARUN 22-JUN-14
Dan Morgan Dan Dangerous ARUN 22-JUN-14

am seeing abv output in sql deveplr.
Re: Getting duplicate entry in audit table after triggered [message #616882 is a reply to message #616881] Sun, 22 June 2014 03:46 Go to previous messageGo to next message
Lalit Kumar B
Messages: 3174
Registered: May 2013
Location: World Wide on the Web
Senior Member
May be you are getting confused by running the select statements as script. I would suggest repeat the steps in SQL*Plus and copy paste the session back here.
Re: Getting duplicate entry in audit table after triggered [message #616883 is a reply to message #616882] Sun, 22 June 2014 04:14 Go to previous messageGo to next message
aruntutor
Messages: 10
Registered: June 2014
Location: chennai
Junior Member
No, am running only select * from audit_log;

Yes, buddy tried in sqlplus too, facing same issue .
Re: Getting duplicate entry in audit table after triggered [message #616886 is a reply to message #616883] Sun, 22 June 2014 04:36 Go to previous messageGo to next message
Lalit Kumar B
Messages: 3174
Registered: May 2013
Location: World Wide on the Web
Senior Member
aruntutor wrote on Sun, 22 June 2014 14:44
No, am running only select * from audit_log;
Yes, buddy tried in sqlplus too, facing same issue .


Don't say you did this and that. SHOW using copy paste from SQL*Plus and use code tags.

By looking at Dan Morgan's name, I can say it is an example from PSOUG, ok got it, it is about Row Level Trigger With Referencing Clause. The example shown there is :

CREATE TABLE person (
fname VARCHAR2(15),
lname VARCHAR2(15));

CREATE TABLE audit_log (
o_fname   VARCHAR2(15),
o_lname   VARCHAR2(15),
n_fname   VARCHAR2(15),
n_lname   VARCHAR2(15),
chng_by   VARCHAR2(10),
chng_when DATE);

CREATE OR REPLACE TRIGGER referencing_clause
AFTER UPDATE
ON person
REFERENCING NEW AS NEW OLD AS OLD
FOR EACH ROW
BEGIN
  INSERT INTO audit_log
  (o_fname, o_lname, n_fname, n_lname, chng_by, chng_when)
  VALUES
  (:OLD.fname, :OLD.lname, :NEW.fname, :NEW.lname, USER, SYSDATE);
END referencing_clause;
/

INSERT INTO person (fname, lname) VALUES ('Dan', 'Morgan');

SELECT * FROM person;
SELECT * FROM audit_log;

UPDATE person
SET lname = 'Dangerous';

SELECT * FROM person;
SELECT * FROM audit_log;

UPDATE person
SET fname = 'Mark', lname = 'Townsend';

SELECT * FROM person;
SELECT * FROM audit_log;


Open SQL*Plus, Drop the existing objects, and execute the code snippet. Copy and paste the entire session.



Regards,
Lalit
Re: Getting duplicate entry in audit table after triggered [message #616888 is a reply to message #616886] Sun, 22 June 2014 04:59 Go to previous message
dariyoosh
Messages: 538
Registered: March 2009
Location: France
Senior Member
Anyone who suggested this example, didn't take into account the fact that if for some reason the transaction firing the trigger is rolled back then no row is inserted into audit_log.

The logging should be done inside a procedure defined as an autonomous transaction which is called from the trigger.
Previous Topic: Table Design Consideration
Next Topic: sysdate functioning
Goto Forum:
  


Current Time: Thu Apr 25 00:06:54 CDT 2024