Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

How to rollback a trigger when transaction rolls back?

936241May 9 2012 — edited May 10 2012
Hello

I need to create an audit trigger, to log all the deletes from a base table CLNT_BOOK. The trigger will copy the deleted row plus some additional info like username and date into the log table BOOK_log. The trigger has to roll back if the delete process rolls back. If I am not mistaken, a trigger should automatically roll back when the transaction rolls back. So I don't really have to write any code to accomplish that, right?
However, it doesn't look so.
Here is my trigger:

CREATE OR REPLACE TRIGGER BOOK_DEL
before DELETE
ON CLNT_BOOK
FOR EACH ROW

DECLARE
--PRAGMA AUTONOMOUS_TRANSACTION;
v_DELETE CHAR(1) := 'D';
v_username varchar2(10);
v_sys_error NUMBER := 0;

BEGIN
SELECT user INTO v_username FROM dual;

INSERT INTO BOOK_log
VALUES ( :OLD.ACCOUNT_NUMBER , :OLD.AMOUNT , :OLD.QTY , :OLD.PROCESS_DATE , :OLD.STATUS , :OLD.LAST_UPDATED_DATE, :OLD.UPDATED_BY , v_DELETE, v_username , SYSDATE , SYS_GUID() , to_char(sysdate, 'HH:MM:SS'));
-- COMMIT;
EXCEPTION
WHEN OTHERS THEN
v_sys_error := SQLCODE;
END;
/


I delete rows from base table, then roll back.

delete CLNT_BOOK where ENT_ID ='0001A'
/
rollback work
/

However the deleted rows stay deleted, and when I select from log table, select * from BOOK_log, the deleted rows are in the log table.
What am I missing here??? Please help!

Thanks in advance
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jun 7 2012
Added on May 9 2012
17 comments
10,240 views