代码之家  ›  专栏  ›  技术社区  ›  Zampanò

复合触发器的before语句和after语句部分的用途

  •  1
  • Zampanò  · 技术社区  · 7 年前

    据我所知,复合触发器的before语句部分用于初始化不需要使用的占位符: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语句部分用于处理可变的表错误。

    此外,我相信我们可以添加在所有行被评估为适当的部分之前和之后希望发生的任何其他操作。

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

    1 回复  |  直到 7 年前
        1
  •  1
  •   kara    7 年前

    在创建 compound trigger 你应该想想你想做什么。每个 复合触发器 可以拆分为多个 triggers . 如果你有多重 触发器 在一 table ,这就是要考虑的一点 复合触发器 ,将这些功能合并为一个 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;