0

Is it possible to create only one trigger in Oracle that fires before and after data got inserted into a table?

begin

if inserting THEN
   -- This code should be run before insertion:
   :new.cr_timestamp := SYSTIMESTAMP;

   IF :NEW.id IS NULL THEN
      SELECT sq_table_id.NEXTVAL
      INTO :NEW.id
      FROM dual;
   END IF;

-- Now I want code which run after the insertion..

end if;

end;

So is there a flag which indicates if its before or after the insertion?

ZerOne
  • 1,296
  • 6
  • 20
  • 40

1 Answers1

4

It can be written as below.

CREATE OR REPLACE TRIGGER compound_trigger_name
FOR [INSERT|DELETE|UPDATE [OF column] ON table
COMPOUND TRIGGER

--Executed before DML statement
     BEFORE STATEMENT IS
     BEGIN
       NULL;
     END BEFORE STATEMENT;

   --Executed aftereach row change- :NEW, :OLD are available
     AFTER EACH ROW IS
     BEGIN
       NULL;
     END AFTER EACH ROW;

     --Executed after DML statement
     AFTER STATEMENT IS
     BEGIN
       NULL;
     END AFTER STATEMENT;

END compound_trigger_name;
XING
  • 9,608
  • 4
  • 22
  • 38