Can we use commit or rollback in trigger? If yes, then how. Please explain with a suitable example?

Answer Posted / santosh kumar

Yes ,You can Commit inside the trigger.

But for this you have to make this trigger transaction to be a Independent transaction from its parent transaction, You can do this by using Pragma. Pragma AUTONOMOUS_TRANSACTION allow you to build the Indepadent(child) Transaction,started by another. Shold be declare in DECLARE section of any subprogram.

Used to make Modular and Resuable Blocks. if you need the example then ask to me.

CREATE OR REPLACE TRIGGER TRIG_ARG
AFTER INSERT ON TAB1
DECLARE
PRAGMA AUTONOMOUNS_TRNASACTION
BEGIN
INSERT INTO LOG VALUES(SYSDATE,'INSERT ON TAB1');
COMMIT;
END;
/

OR

CREATE OR REPLACE TRIGGER t_trigger
AFTER INSERT ON t1 FOR EACH ROW

DECLARE
PRAGMA AUTONOMOUS_TRANSACTION;
i PLS_INTEGER;
BEGIN
SELECT COUNT(*)
INTO i
FROM t1;

INSERT INTO t2
VALUES
(i);
COMMIT;
END;
/

Is This Answer Correct ?    0 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What port does sql server use?

610


List the various privileges that a user can grant to another user?

671


What is set serveroutput on in pl sql?

689


Explain exception handling in pl/sql?

610


What are different functions in sql?

604






Explain the difference in execution of triggers and stored procedures?

661


Why do we create views in sql?

659


Why stored procedure is faster than query?

601


Can a trigger call a stored procedure?

648


Can we commit in trigger?

630


What is the use of double ampersand (&&) in sql queries? Give an example

707


How do you declare a user-defined exception?

631


Is sql open source?

617


What does truncate mean in sql?

631


What are different types of sql commands?

603