Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1922
  • Last Modified:

Roll back with trigger

How can I execute a Rollback statement with the use of a trigger? I know that I can not place a rollback statemnet in a trigger but how can I work around this obstacle.
0
rkm123
Asked:
rkm123
1 Solution
 
ser6398Commented:
An unhandled exception in a trigger automatically rolls back the trigger body and the triggering statement.  If a predefined or user-defined error condition or exception is raised during the execution of a trigger
body, then all effects of the trigger body, as well as the triggering statement, are rolled back (unless
the error is trapped by an exception handler (use RAISE_APPLICATION_ERROR if handled)). Therefore, a trigger body can prevent the execution of the triggering statement by raising an exception. User-defined exceptions are commonly used in triggers that enforce complex security or constraints.  ONLY the triggering statement is rolled back => a commit saves any SQL made before that statement.

So, if you want to rollback, you can do something like:

declare
  ex_my_exception   EXCEPTION;
begin
  IF (user = 'BOB')
  THEN RAISE ex_my_exception;
  ELSE do_something_else;
  END IF;
end;

If you don't handle the exception, it will cause a rollback.
0
 
ser6398Commented:
By the way, if you don't want the trigger to rollback the initiating statement, you can use PRAGMA AUTONOMOUS_TRANSACTION to make the trigger function within it's own transaction, so you can rollback only the trigger body, and not the initiating statement:

CREATE OR REPLACE TRIGGER ins_emp
   AFTER INSERT
   ON emp
 FOR EACH ROW
DECLARE
   PRAGMA AUTONOMOUS_TRANSACTION;
   vsal   NUMBER;
BEGIN
   ...

0
 
MindphaserCommented:
Please update and finalize this old, open question. Please:

1) Award points ... if you need Moderator assistance to split points, comment here with details please or advise us in Community Support with a zero point question and this question link.
2) Ask us to delete it if it has no value to you or others
3) Ask for a refund so that we can move it to our PAQ at zero points if it did not help you but may help others.

EXPERT INPUT WITH CLOSING RECOMMENDATIONS IS APPRECIATED IF ASKER DOES NOT RESPOND.

Thanks,

** Mindphaser - Community Support Moderator **

P.S.  Click your Member Profile, choose View Question History to go through all your open and locked questions to update them.
0
 
DanRollinsCommented:
Recommended disposition:

    Accept ser6398's comment(s) as an answer.

DanRollins -- EE database cleanup volunteer
0
 
MoondancerCommented:
Thanks, Dan.
I finalized this today and will monitor it in the event an adjustment is needed.
Moondancer - EE Moderator
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now