• Oracle 11g Compound Trigger


    Original Link

    In Oracle 11g, the concept of compound trigger was introduced. A compound trigger is a single trigger on a table that enables you to specify actions for each of four timing points:

    1. Before the firing statement
    2. Before each row that the firing statement affects
    3. After each row that the firing statement affects
    4. After the firing statement

    With the compound trigger, both the statement-level and row-level action can be put up in a single trigger. Plus there is an added advantage: it allows sharing of common state between all the trigger-points using variable. This is because compound trigger in oracle 11g has a declarative section where one can declare variable to be used within trigger. This common state is established at the start of triggering statement and is destroyed after completion of trigger (regardless of trigger being in error or not). If same had to be done without compound-trigger, it might have been required to share data using packages.

    When to use Compound Triggers

    The compound trigger is useful when you want to accumulate facts that characterize the “for each row” changes and then act on them as a body at “after statement” time. Two popular reasons to use compound trigger are:

    1. To accumulate rows for bulk-insertion. We will later see an example for this.
    2. To avoid the infamous ORA-04091: mutating-table error.

      Details of Syntax

    CREATE OR REPLACE TRIGGER compound_trigger_name
    FOR [INSERT|DELETE]UPDATE [OF column] ON table
    COMPOUND TRIGGER
       -- Declarative Section (optional)
       -- Variables declared here have firing-statement duration.
         
         --Executed before DML statement
         BEFORE STATEMENT IS
         BEGIN
           NULL;
         END BEFORE STATEMENT;
       
         --Executed before each row change- :NEW, :OLD are available
         BEFORE EACH ROW IS
         BEGIN
           NULL;
         END BEFORE EACH ROW;
       
         --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;
    View Code

    Note the ‘COMPOUND TRIGGER’ keyword above.

    Some Restriction/Catches to note

    1. The body of a compound trigger must be a compound trigger block.
    2. A compound trigger must be a DML trigger.
    3. A compound trigger must be defined on either a table or a view.
    4. The declarative part cannot include PRAGMA AUTONOMOUS_TRANSACTION.
    5. A compound trigger body cannot have an initialization block; therefore, it cannot have an exception section. This is not a problem, because the BEFORE STATEMENT section always executes exactly once before any other timing-point section executes.
    6. An exception that occurs in one section must be handled in that section. It cannot transfer control to another section.
    7. If a section includes a GOTO statement, the target of the GOTO statement must be in the same section.
    8. OLD, :NEW, and :PARENT cannot appear in the declarative part, the BEFORE STATEMENT section, or the AFTER STATEMENT section.
    9. Only the BEFORE EACH ROW section can change the value of :NEW.
    10. If, after the compound trigger fires, the triggering statement rolls back due to a DML exception:
      • Local variables declared in the compound trigger sections are re-initialized, and any values computed thus far are lost.
      • Side effects from firing the compound trigger are not rolled back.
    11. The firing order of compound triggers is not guaranteed. Their firing can be interleaved with the firing of simple triggers.
    12. If compound triggers are ordered using the FOLLOWS option, and if the target of FOLLOWS does not contain the corresponding section as source code, the ordering is ignored.
  • 相关阅读:
    logging模块-logging.basicConfig、logger.setLevel、handler.setLevel优先级
    Python标准模块--logging(转载)
    第三节,入门知识和windows系统安装python环境
    第二节windows系统下Xshell 5软件远程访问虚拟机 Linux系统
    第一节windows系统安装虚拟机VMware 软件
    数学数列
    java String
    linux eclipse 报错过时的方法
    java.util.Random 类
    java uitl
  • 原文地址:https://www.cnblogs.com/dufu/p/6479245.html
Copyright © 2020-2023  润新知