tags:

views:

64

answers:

1

I want to update the same record which fires a trigger. I have done that using "BEFORE INSERT" option. But note that I have use a transaction to rollback the operation if there is an any faliure.

CREATE OR REPLACE TRIGGER GANUKA.INTF_CONTROLLER_UPLOADER
BEFORE insert ON GANUKA.INTF_CONTROLLER for each row
DECLARE 
    PRAGMA AUTONOMOUS_TRANSACTION;
    max_id INTEGER;
    stat VARCHAR2(32);
begin

select :new.id into max_id from dual;
select :new.status into stat from dual;



 IF STAT = 'NEW' THEN   --ONLY NEW UPLOADS WILL CONTINUE FOR PROCESS
    :NEW.STATUS := 'STARTED';
     max_id := GANUKA.BACKOFFICE_UPDATE(max_id); --PL/SQL function
    :NEW.STATUS := 'COMPLETED';

ELSE
   :NEW.STATUS := 'ABORTED';
    :NEW.REMARKS :='STATUS IS NOT RECONGNIZED';

END IF;

COMMIT;

EXCEPTION
    WHEN OTHERS
     THEN
       ROLLBACK;
        RAISE;

end;
/

Problem is if there is an an any exception I want to update the record to set the state as 'Failed'. Can any one tell me how to do that.

A: 

I'm not sure why do you use autonomous transaction here and why do you have to commit/rollback in the trigger...

andr
within the PL/SQL function there are several data transactions between tables. So I need to make sure that data transfer has occur without any problem. If there is any failure I need to roll back whole process. But need to keep the record which fires the trigger and update it status as 'FAILED'
ganuke
Then perform the `INSERT` first, call the PL/SQL function. If there is a failue then rollback, followed by another `INSERT` marked as FAILED. Triggers are semi-evil, Autonomous Transactions are semi-evil. Combining them is full-evil!
Adam Hawkes