articles 11g trigger enhancements 11gr1

Upload: balajismith

Post on 14-Apr-2018

218 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/30/2019 Articles 11g Trigger Enhancements 11gr1

    1/9

    Home Articles Scripts Forums Blog Certification Misc Search About Printer Friendly

    Like 0 Tweet 0 1 Search

    Oracle 8i | Oracle 9i | Oracle 10g | Orac le 11g | Oracle 1 2c | Misce llaneous | PL/SQL | SQL | Oracle RAC | Oracle Apps | Linux

    Home Articles 11g Here

    Trigger Enhancements in Oracle Database 11g Release 1

    Execution Order of Trigg ers

    Compo und Trigg ers

    Enable and Disable Trigg ers

    Execution Order of Triggers

    Oracle allows more than one trigger to be created for the same timing point, but it has never guaranteed the

    exe cution o rder of those trigg ers. The Oracle 11g trigg er syntax now includes the FOLLOWS clause to guaranteeexecution order for trigg ers de fined with the s ame timing p oint. The fo llowing exam ple cre ates a table with twotrigg ers for the same timing point.

    CREATE TABLE trigger_follows_test (id NUMBER,description VARCHAR2(50)

    );

    CREATE OR REPLACE TRIGGER trigger_follows_test_trg_1BEFORE INSERT ON trigger_follows_testFOR EACH ROWBEGIN

    DBMS_OUTPUT.put_line('TRIGGER_FOLLOWS_TEST_TRG_1 - Executed');END;/

    CREATE OR REPLACE TRIGGER trigger_follows_test_trg_2BEFORE INSERT ON trigger_follows_testFOR EACH ROWBEGIN

    DBMS_OUTPUT.put_line('TRIGGER_FOLLOWS_TEST_TRG_2 - Executed');END;

    Ads by Google Oracle JDBC Oracle 1

    PDFmyURL.com

    http://www.oracle-base.com/articles/11g/trigger-enhancements-11gr1.php#enable_and_disable_triggershttp://www.oracle-base.com/articles/11g/trigger-enhancements-11gr1.php#compound_triggershttp://www.oracle-base.com/articleshttp://www.oracle-base.com/index.phphttp://www.oracle-base.com/articles/articles.phphttp://www.oracle-base.com/dba/scripts.phphttp://www.oracle-base.com/forums/http://www.oracle-base.com/blog/http://www.oracle-base.com/misc/ocp-certification.phphttp://www.oracle-base.com/index.phphttp://www.oracle-base.com/articles/articles.phphttp://www.oracle-base.com/index.phphttp://pdfmyurl.com/?otsrc=watermark&otclc=0.01http://pdfmyurl.com/?otsrc=watermark&otclc=0.01http://www.oracle-base.com/articles/11g/trigger-enhancements-11gr1.php#enable_and_disable_triggershttp://www.oracle-base.com/articles/11g/trigger-enhancements-11gr1.php#compound_triggershttp://www.oracle-base.com/articles/11g/trigger-enhancements-11gr1.php#execution_order_of_triggershttp://www.oracle-base.com/articles/11g/trigger-enhancements-11gr1.php#http://www.oracle-base.com/articles/11g/trigger-enhancements-11gr1.php#http://www.oracle-base.com/articles/11g/trigger-enhancements-11gr1.php#http://www.oracle-base.com/articles/11g/trigger-enhancements-11gr1.php#http://www.oracle-base.com/articles/11ghttp://www.oracle-base.com/articleshttp://www.oracle-base.com/http://www.oracle-base.com/articles/linux/articles-linux.phphttp://www.oracle-base.com/articles/apps/articles-apps.phphttp://www.oracle-base.com/articles/rac/articles-rac.phphttp://www.oracle-base.com/articles/sql/articles-sql.phphttp://www.oracle-base.com/articles/plsql/articles-plsql.phphttp://www.oracle-base.com/articles/misc/articles-misc.phphttp://www.oracle-base.com/articles/12c/articles-12c.phphttp://www.oracle-base.com/articles/11g/articles-11g.phphttp://www.oracle-base.com/articles/10g/articles-10g.phphttp://www.oracle-base.com/articles/9i/articles-9i.phphttp://www.oracle-base.com/articles/8i/articles-8i.phphttp://www.oracle-base.com/articles/11g/trigger-enhancements-11gr1.php?display_type=printablehttp://www.oracle-base.com/misc/site-info.phphttp://www.oracle-base.com/searchhttp://www.oracle-base.com/misc/miscellaneous.phphttp://www.oracle-base.com/misc/ocp-certification.phphttp://www.oracle-base.com/blog/http://www.oracle-base.com/forums/http://www.oracle-base.com/dba/scripts.phphttp://www.oracle-base.com/articles/articles.phphttp://www.oracle-base.com/index.phphttp://www.oracle-base.com/index.php
  • 7/30/2019 Articles 11g Trigger Enhancements 11gr1

    2/9

    /

    If we insert into the test table, there is no guarantee o f the e xecution order.

    SQL> SET SERVEROUTPUT ONSQL> INSERT INTO trigger_follows_test VALUES (1, 'ONE');TRIGGER_FOLLOWS_TEST_TRG_1 - ExecutedTRIGGER_FOLLOWS_TEST_TRG_2 - Executed

    1 row created.

    SQL>

    We c an spec ify that the TRIGGER_FOLLOWS_TEST_TRG_2 trigg er should be e xec uted before the TRIGGER_FOLLOWS_TEST_TRG_1 trigger by recreating the

    TRIGGER_FOLLOWS_TEST_TRG_1 trigg er using t he FOLLOWS clause.

    CREATE OR REPLACE TRIGGER trigger_follows_test_trg_1BEFORE INSERT ON trigger_follows_testFOR EACH ROWFOLLOWS trigger_follows_test_trg_2

    BEGINDBMS_OUTPUT.put_line('TRIGGER_FOLLOWS_TEST_TRG_1 - Executed');

    END;/

    No w the TRIGGER_FOLLOWS_TEST_TRG_1 trigger always follows the TRIGGER_FOLLOWS_TEST_TRG_2 trigger.

    SQL> SET SERVEROUTPUT ONSQL> INSERT INTO trigger_follows_test VALUES (2, 'TWO');TRIGGER_FOLLOWS_TEST_TRG_2 - ExecutedTRIGGER_FOLLOWS_TEST_TRG_1 - Executed

    1 row created.

    SQL>

    Don't forget to clean up the test table.

    DROP TABLE trigger_follows_test;

    PDFmyURL.com

    http://pdfmyurl.com/?otsrc=watermark&otclc=0.01http://pdfmyurl.com/?otsrc=watermark&otclc=0.01
  • 7/30/2019 Articles 11g Trigger Enhancements 11gr1

    3/9

    Compound Triggers

    A compound trigger allows code for one or more timing points for a specific object to be combined into a single trigger. The individual timing points can share a single global

    dec laration section, whose s tate is maintained for the lifetime of the stateme nt. Once a s tatement ends, due to s ucces sful completion or an error, the trigg er state is cleanedup. In previous releases this type of functionality was only possible by defining multiple triggers whose code and global variables were defined in a separate package, as shown

    in the Mutating Table Exceptions article, but the compound trigger allows for a much tidier solution.

    The trigg ering actions are def ined in the same way as any other DML trigg er, with the addition o f the COMPOUND TRIGGER clause. T he main body o f the trigg er is made up

    of an optional global declaration section and one or more timing point sections, each of which may contain a local declaration section whose state is not maintained.

    CREATE OR REPLACE TRIGGER FOR ON

    COMPOUND TRIGGER

    -- Global declaration.g_global_variable VARCHAR2(10);

    BEFORE STATEMENT ISBEGIN

    NULL; -- Do something here.END BEFORE STATEMENT;

    BEFORE EACH ROW ISBEGIN

    NULL; -- Do something here.END BEFORE EACH ROW;

    AFTER EACH ROW ISBEGIN

    NULL; -- Do something here.END AFTER EACH ROW;

    AFTER STATEMENT ISBEGIN

    NULL; -- Do something here.

    END AFTER STATEMENT;

    END ;/

    The fo llowing co de creates a tes t table and a co mpound trigg er that fires for e ach timing po int asso ciated with insert, update and delete s tatements. The trigge ring actions are

    log ge d in a PL/SQL table de fined in the glo bal declaration sec tion. The final timing p oint for e ach state me nt prints out the c onte nt of the PL/SQL table to show that the variable

    state has been maintained throughout the lifetime of the statement.

    CREATE TABLE compound trigger test (

    PDFmyURL.com

    http://pdfmyurl.com/?otsrc=watermark&otclc=0.01http://pdfmyurl.com/?otsrc=watermark&otclc=0.01http://www.oracle-base.com/articles/9i/mutating-table-exceptions.php
  • 7/30/2019 Articles 11g Trigger Enhancements 11gr1

    4/9

    _ _id NUMBER,description VARCHAR2(50)

    );

    CREATE OR REPLACE TRIGGER compound_trigger_test_trgFOR INSERT OR UPDATE OR DELETE ON compound_trigger_test

    COMPOUND TRIGGER

    -- Global declaration.TYPE t_tab IS TABLE OF VARCHAR2(50);

    l_tab t_tab := t_tab();

    BEFORE STATEMENT ISBEGIN

    l_tab.extend;CASE

    WHEN INSERTING THENl_tab(l_tab.last) := 'BEFORE STATEMENT - INSERT';

    WHEN UPDATING THENl_tab(l_tab.last) := 'BEFORE STATEMENT - UPDATE';

    WHEN DELETING THENl_tab(l_tab.last) := 'BEFORE STATEMENT - DELETE';

    END CASE;END BEFORE STATEMENT;

    BEFORE EACH ROW ISBEGIN

    l_tab.extend;CASE

    WHEN INSERTING THENl_tab(l_tab.last) := 'BEFORE EACH ROW - INSERT (new.id=' || :new.id || ')';

    WHEN UPDATING THENl_tab(l_tab.last) := 'BEFORE EACH ROW - UPDATE (new.id=' || :new.id || ' old.id=' || :old.id || ')';

    WHEN DELETING THENl_tab(l_tab.last) := 'BEFORE EACH ROW - DELETE (old.id=' || :old.id || ')';

    END CASE;END BEFORE EACH ROW;

    AFTER EACH ROW ISBEGIN

    l_tab.extend;CASE

    WHEN INSERTING THENl_tab(l_tab.last) := 'AFTER EACH ROW - INSERT (new.id=' || :new.id || ')';

    WHEN UPDATING THENl_tab(l_tab.last) := 'AFTER EACH ROW - UPDATE (new.id=' || :new.id || ' old.id=' || :old.id || ')';

    PDFmyURL.com

    http://pdfmyurl.com/?otsrc=watermark&otclc=0.01http://pdfmyurl.com/?otsrc=watermark&otclc=0.01
  • 7/30/2019 Articles 11g Trigger Enhancements 11gr1

    5/9

    WHEN DELETING THENl_tab(l_tab.last) := 'AFTER EACH ROW - DELETE (old.id=' || :old.id || ')';

    END CASE;END AFTER EACH ROW;

    AFTER STATEMENT ISBEGIN

    l_tab.extend;CASE

    WHEN INSERTING THEN

    l_tab(l_tab.last) := 'AFTER STATEMENT - INSERT';WHEN UPDATING THEN

    l_tab(l_tab.last) := 'AFTER STATEMENT - UPDATE';WHEN DELETING THEN

    l_tab(l_tab.last) := 'AFTER STATEMENT - DELETE';END CASE;

    FOR i IN l_tab.first .. l_tab.last LOOP

    DBMS_OUTPUT.put_line(l_tab(i));END LOOP;l_tab.delete;

    END AFTER STATEMENT;

    END compound_trigger_test_trg;/

    By issuing se veral insert, update and delete stateme nts against the te st table we c an see that the co mpound trigge r is working as e xpecte d.

    SQL> SET SERVEROUTPUT ONSQL> INSERT INTO compound_trigger_test VALUES (1, 'ONE');BEFORE STATEMENT - INSERTBEFORE EACH ROW - INSERT (new.id=1)AFTER EACH ROW - INSERT (new.id=1)AFTER STATEMENT - INSERT

    1 row created.

    SQL> INSERT INTO compound_trigger_test VALUES (2, 'TWO');BEFORE STATEMENT - INSERTBEFORE EACH ROW - INSERT (new.id=2)AFTER EACH ROW - INSERT (new.id=2)AFTER STATEMENT - INSERT

    1 row created.

    PDFmyURL.com

    http://pdfmyurl.com/?otsrc=watermark&otclc=0.01http://pdfmyurl.com/?otsrc=watermark&otclc=0.01
  • 7/30/2019 Articles 11g Trigger Enhancements 11gr1

    6/9

    SQL> UPDATE compound_trigger_test SET id = id;BEFORE STATEMENT - UPDATEBEFORE EACH ROW - UPDATE (new.id=2 old.id=2)AFTER EACH ROW - UPDATE (new.id=2 old.id=2)BEFORE EACH ROW - UPDATE (new.id=1 old.id=1)AFTER EACH ROW - UPDATE (new.id=1 old.id=1)AFTER STATEMENT - UPDATE

    2 rows updated.

    SQL> DELETE FROM compound_trigger_test;BEFORE STATEMENT - DELETEBEFORE EACH ROW - DELETE (old.id=2)AFTER EACH ROW - DELETE (old.id=2)BEFORE EACH ROW - DELETE (old.id=1)AFTER EACH ROW - DELETE (old.id=1)AFTER STATEMENT - DELETE

    2 rows deleted.

    SQL>

    Don't forget to clean up the test table.

    DROP TABLE compound_trigger_test;

    For a mo re practical use o f co mpound trigge rs, we c an take the example quoted in the Mutating Table Exceptions article and replace the two triggers and the package with a

    single compound trigger, as shown below.

    CREATE OR REPLACE TRIGGER tab1_compound_triggerFOR INSERT OR UPDATE ON tab1

    COMPOUND TRIGGER

    TYPE t_change_tab IS TABLE OF tab1_audit%ROWTYPE;g_change_tab t_change_tab := t_change_tab();

    AFTER EACH ROW ISBEGIN

    g_change_tab.extend;g_change_tab(g_change_tab.last).id := tab1_audit_seq.NEXTVAL;IF INSERTING THEN

    g_change_tab(g_change_tab.last).action := 'INSERT';ELSE

    g_change_tab(g_change_tab.last).action := 'UPDATE';

    PDFmyURL.com

    http://pdfmyurl.com/?otsrc=watermark&otclc=0.01http://pdfmyurl.com/?otsrc=watermark&otclc=0.01http://www.oracle-base.com/articles/9i/mutating-table-exceptions.php
  • 7/30/2019 Articles 11g Trigger Enhancements 11gr1

    7/9

    END IF;g_change_tab(g_change_tab.last).tab1_id := :new.id;g_change_tab(g_change_tab.last).created_time := SYSTIMESTAMP;

    END AFTER EACH ROW;

    AFTER STATEMENT ISl_count NUMBER(10);

    BEGINFOR i IN g_change_tab.first .. g_change_tab.last LOOP

    SELECT COUNT(*)

    INTO g_change_tab(i).record_countFROM tab1;

    END LOOP;

    FORALL i IN g_change_tab.first .. g_change_tab.lastINSERT INTO tab1_audit VALUES g_change_tab(i);

    g_change_tab.delete;END AFTER STATEMENT;

    END tab1_compound_trigger;/

    From a timing point perspective, the Compo und Trigg er Restrictions follow very closely with those of individual statement and row level triggers. The main point of interest

    here is the co ntrol o f exe cution o rder. If multiple co mpound trigg ers are de fined f or the same object, their order of exec ution can be co ntrolled using the FOLLOWS clause,

    but this cannot be use d to co ntrol ex ec ution o rder when bo th co mpo und and reg ular DML trigg ers are de fined ag ainst a single objec t. In such situations it is be tte r to s tick with

    all DML triggers, or all compound triggers.

    Enable and Disable Triggers

    It has bee n possible to enable and disable trigge rs for so me time using the ALTER TRIGGER and ALTER TABLE commands.

    ALTER TRIGGER DISABLE;

    ALTER TRIGGER ENABLE;

    ALTER TABLE DISABLE ALL TRIGGERS;

    ALTER TABLE ENABLE ALL TRIGGERS;

    Prior to 11g, it was only possible to create triggers in the enabled state, then subsequently disable them. Now they can be explicitly enabled or disabled at creation time, with

    the enabled state as the default.

    CREATE TABLE trigger_control_test (id NUMBER,description VARCHAR2(50)

    );

    PDFmyURL.com

    http://pdfmyurl.com/?otsrc=watermark&otclc=0.01http://pdfmyurl.com/?otsrc=watermark&otclc=0.01http://docs.oracle.com/cd/B28359_01/appdev.111/b28370/triggers.htm#CIHDFGEH
  • 7/30/2019 Articles 11g Trigger Enhancements 11gr1

    8/9

    CREATE OR REPLACE TRIGGER trigger_control_test_trgBEFORE INSERT ON trigger_control_testFOR EACH ROWENABLEBEGIN

    DBMS_OUTPUT.put_line('TRIGGER_CONTROL_TEST_TRG - Executed');END;/

    SQL> SET SERVEROUTPUT ON

    SQL> INSERT INTO trigger_control_test VALUES (1, 'ONE');TRIGGER_CONTROL_TEST_TRG - Executed

    1 row created.

    SQL>

    CREATE OR REPLACE TRIGGER trigger_control_test_trgBEFORE INSERT ON trigger_control_testFOR EACH ROWDISABLEBEGIN

    DBMS_OUTPUT.put_line('TRIGGER_CONTROL_TEST_TRG - Executed');

    END;/

    SQL> INSERT INTO trigger_control_test VALUES (2, 'TWO');

    1 row created.

    SQL>

    Don't forget to clean up the test table.

    DROP TABLE trigger_control_test;

    For more information see:

    What's New in PL/SQL? - Oracle Database PL/SQL Language Reference 11g Release 1 (11.1)CREATE TRIGGER

    Compo und Trigg ers

    Hope this helps. Regards Tim...

    Back to the Top.

    PDFmyURL.com

    http://pdfmyurl.com/?otsrc=watermark&otclc=0.01http://pdfmyurl.com/?otsrc=watermark&otclc=0.01http://www.oracle-base.com/articles/11g/trigger-enhancements-11gr1.php#Tophttp://docs.oracle.com/cd/B28359_01/appdev.111/b28370/triggers.htm#CIHEFGFDhttp://docs.oracle.com/cd/B28359_01/server.111/b28286/statements_7004.htmhttp://docs.oracle.com/cd/B28359_01/appdev.111/b28370/whatsnew.htm#CJAEGHHH
  • 7/30/2019 Articles 11g Trigger Enhancements 11gr1

    9/9

    Like 0 Tweet 0 1

    7 co mments, read/add them...

    Home | Articles | Scripts | Forums | Blog | Certification | Misc | Search | About

    Copyright & Disclaimer

    HTMLCSS

    PDFmyURL.com

    http://pdfmyurl.com/?otsrc=watermark&otclc=0.01http://pdfmyurl.com/?otsrc=watermark&otclc=0.01http://jigsaw.w3.org/css-validator/check/refererhttp://validator.w3.org/check?uri=refererhttp://www.oracle-base.com/misc/site-info.php#copyrighthttp://www.oracle-base.com/misc/site-info.phphttp://www.oracle-base.com/search/http://www.oracle-base.com/misc/miscellaneous.phphttp://www.oracle-base.com/misc/ocp-certification.phphttp://www.oracle-base.com/blog/http://www.oracle-base.com/forums/http://www.oracle-base.com/dba/scripts.phphttp://www.oracle-base.com/articles/articles.phphttp://www.oracle-base.com/index.phphttp://www.oracle-base.com/articles/11g/trigger-enhancements-11gr1.php#http://www.oracle-base.com/articles/11g/trigger-enhancements-11gr1.php#http://www.oracle-base.com/articles/11g/trigger-enhancements-11gr1.php#http://www.oracle-base.com/articles/11g/trigger-enhancements-11gr1.php#http://www.oracle-base.com/misc/comments.php?page_id=773