Solved

Roll back with trigger

Posted on 2001-07-16
5
1,914 Views
Last Modified: 2010-01-18
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
Comment
Question by:rkm123
5 Comments
 
LVL 5

Expert Comment

by:ser6398
ID: 6287392
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
 
LVL 5

Accepted Solution

by:
ser6398 earned 20 total points
ID: 6287413
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
 
LVL 6

Expert Comment

by:Mindphaser
ID: 7038131
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
 
LVL 49

Expert Comment

by:DanRollins
ID: 7053174
Recommended disposition:

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

DanRollins -- EE database cleanup volunteer
0
 
LVL 1

Expert Comment

by:Moondancer
ID: 7054295
Thanks, Dan.
I finalized this today and will monitor it in the event an adjustment is needed.
Moondancer - EE Moderator
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Oracle 10g - insert string with special characters 8 55
Query Records that don't match 8 42
Repeat query 13 46
ORA-02288: invalid OPEN mode 2 26
Introduction A previously published article on Experts Exchange ("Joins in Oracle", http://www.experts-exchange.com/Database/Oracle/A_8249-Joins-in-Oracle.html) makes a statement about "Oracle proprietary" joins and mixes the join syntax with gen…
How to Create User-Defined Aggregates in Oracle Before we begin creating these things, what are user-defined aggregates?  They are a feature introduced in Oracle 9i that allows a developer to create his or her own functions like "SUM", "AVG", and…
This video shows how to recover a database from a user managed backup
This video explains what a user managed backup is and shows how to take one, providing a couple of simple example scripts.

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question