Solved

Roll back with trigger

Posted on 2001-07-16
5
1,913 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Recommended disposition:

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

DanRollins -- EE database cleanup volunteer
0
 
LVL 1

Expert Comment

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

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

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…
I remember the day when someone asked me to create a user for an application developement. The user should be able to create views and materialized views and, so, I used the following syntax: (CODE) This way, I guessed, I would ensure that use…
This video shows how to copy a database user from one database to another user DBMS_METADATA.  It also shows how to copy a user's permissions and discusses password hash differences between Oracle 10g and 11g.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…

771 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now