zoukankan      html  css  js  c++  java
  • 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.
  • 相关阅读:
    UWP开发-获取设备唯一ID
    html5加js实现本地文件读取和写入并获取本地文件路径
    C/C++杂记:运行时类型识别(RTTI)与动态类型转换原理
    C/C++杂记:深入虚表结构
    C/C++杂记:虚函数的实现的基本原理
    C/C++杂记:深入理解数据成员指针、函数成员指针
    C/C++杂记:NULL与0的区别、nullptr的来历
    细说:Unicode, UTF-8, UTF-16, UTF-32, UCS-2, UCS-4
    汉字编码:GB2312, GBK, GB18030, Big5
    ANSI是什么编码?
  • 原文地址:https://www.cnblogs.com/dufu/p/6479245.html
Copyright © 2011-2022 走看看