插入前后的Oracle触发器

时间:2016-07-12 13:01:40

标签: oracle oracle11g database-trigger

是否有可能在Oracle中只创建一个在数据插入表之前和之后触发的触发器?

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;

那么是否有一个标志指示它在插入之前或之后?

1 个答案:

答案 0 :(得分:4)

可以写成如下。

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;