Oracle After Delete Trigger… How to avoid Mutating Table (ORA-04091)?

前端 未结 2 1653
太阳男子
太阳男子 2020-12-06 19:57

Let\'s say we have the following table structures:

documents      docmentStatusHistory      status
+---------+   +--------------------+    +----------+
| doc         


        
相关标签:
2条回答
  • 2020-12-06 20:33

    The standard workaround to a mutating table error is to create

    • A package with a collection of keys (i.e. docId's in this case). A temporary table would also work
    • A before statement trigger that initializes the collection
    • A row-level trigger that populates the collection with each docId that has changed
    • An after statement trigger that iterates over the collection and does the actual UPDATE

    So something like

    CREATE OR REPLACE PACKAGE pkg_document_status
    AS
      TYPE typ_changed_docids IS TABLE OF documentos.docId%type;
      changed_docids typ_changed_docids := new typ_changed_docids ();
    
      <<other methods>>
    END;
    
    CREATE OR REPLACE TRIGGER trg_init_collection
      BEFORE DELETE ON documentStatusHistory
    BEGIN
      pkg_document_status.changed_docids.delete();
    END;
    
    CREATE OR REPLACE TRIGGER trg_populate_collection
      BEFORE DELETE ON documentStatusHistory
      FOR EACH ROW
    BEGIN
      pkg_document_status.changed_docids.extend();
      pkg_document_status.changed_docids( pkg_document_status.changed_docids.count() ) := :old.docId;
    END;
    
    CREATE OR REPLACE TRIGGER trg_use_collection
      AFTER DELETE ON documentStatusHistory
    BEGIN
      FOR i IN 1 .. pkg_document_status.changed_docids.count()
      LOOP
        <<fix the current status for pkg_document_status.changed_docids(i) >>
      END LOOP;
      pkg_document_status.changed_docids.delete();
    END;
    
    0 讨论(0)
  • 2020-12-06 20:42

    seems to be a duplicate of this question

    check out Tom Kyte's take on that

    0 讨论(0)
提交回复
热议问题