复合触发器的Before语句和语句后部分的用途

时间:2018-05-22 17:59:03

标签: oracle plsql triggers

根据我的理解,复合触发器的Before Statement部分用于初始化不需要使用的占位符:NEW或:OLD要初始化。这里可以看到一个例子:

CREATE OR REPLACE TRIGGER zipcode_compound
    FOR INSERT OR UPDATE
    ON zipcode
    COMPOUND TRIGGER
    v_date   DATE;
    v_user   VARCHAR2 (30);

    BEFORE STATEMENT
    IS
    BEGIN
        v_date := SYSDATE;
        v_user := USER;
    END BEFORE STATEMENT;
END zipcode_compound;

同样根据我的理解,复合触发器的After Statement部分用于处理变异表错误。

此外,我相信我们可以在所有行被评估到适当的部分之前和之后添加我们想要发生的任何其他内容。

我的理解是否正确?如果没有,请说明我不理解的内容。

1 个答案:

答案 0 :(得分:1)

在创建compound trigger之前,您应该考虑一下您想要做什么。每个compound trigger都可以拆分为多个triggers。如果您在triggers上有多个table,则需要考虑compound trigger,将这些功能合并为一个trigger

这是Toad为Compound triggers创建的默认模板 - 它解释得非常好:

COMPOUND TRIGGER
    tmpVar NUMBER;
  BEFORE STATEMENT IS
  BEGIN
    begin
      -- we cannot reference :new or :old in the before statement section
      tmpVar := 0;
    EXCEPTION
      WHEN OTHERS THEN
       -- Consider logging the error and then re-raise
      RAISE;
    END;
  END BEFORE STATEMENT;

  BEFORE EACH ROW IS
  BEGIN
    begin
      -- we can read or write to :new or :old in the before each row section
      tmpVar := 0;
    EXCEPTION
      WHEN OTHERS THEN
       -- Consider logging the error and then re-raise
      RAISE;
    END;
  END BEFORE EACH ROW;

  AFTER EACH ROW IS
  BEGIN
    begin
      tmpVar := 0;
      -- we can read, but not write to :new or :old in the after each row section
    EXCEPTION
      WHEN OTHERS THEN
       -- Consider logging the error and then re-raise
      RAISE;
    END;
  END AFTER EACH ROW;

  AFTER STATEMENT IS
  BEGIN
    begin
      -- we cannot reference :new or :old in the after statement section
      tmpVar := 0;
    EXCEPTION
      WHEN OTHERS THEN
       -- Consider logging the error and then re-raise
      RAISE;
    END;
  END AFTER STATEMENT;
END MyTrigger;