Link to home
Start Free TrialLog in
Avatar of rkm123
rkm123

asked on

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.
Avatar of ser6398
ser6398

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.
ASKER CERTIFIED SOLUTION
Avatar of ser6398
ser6398

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
Avatar of DanRollins
Recommended disposition:

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

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