Problem
In the forum often encounter the problem of variation table, I collected a list of questions about the variation of the article, now put him here, hope to share with you!
Avoiding mutating Tables
Ok, so you ' ve just recieved the error:
Ora-04091:table XXXX is mutating, trigger/function could not
And you want to get around that. This short article would describe and demonstrate the various methods of getting around the mutating table error.
If you are are interested in why your are getting it and in what you'll get it, please to the Oracle Server cases N Developers Guide (click here to read it right now – this link is to technet.oracle.com. You are need a password to access this site but your can get one right away for free).
Avoiding the mutating table error is fairly easy. We must defer processing against the mutating or constrainng table until a after trigger. We'll consider two cases:
Hitting the ORA-4091 in a INSERT trigger or an UPDATE trigger where and need access to the:new values
Hitting the ORA-4091 in a deletes trigger or an UPDATE trigger where and need to access the:old values
Case 1-you only need to access the:new values
This is the simplest. What We are capture the rowids of the inserted or udpated rows. We can then use this rowids in a after trigger into query up the affected rows.
It always takes 3 triggers to work around the mutating table error. They are:
A before trigger to set the package state to a known, consistent state
A after, row level trigger to capture each rows changes
An after trigger to actually process the change.
As an example--to show you to do this, we'll attempt to answer the following question:
I have a table containing a key/status/effective date combination. When status
Changes, the values are propagated by trigger to a log table recording the
Status history. When no RI constraint was in place everything works fine.
When a RI trigger enforces a parent-child relationship, the status change
Logging trigger fails because the parent table is mutating. Propagating the
Values to the Log table implicitly generates a lookup back to the parent table
To ensure the RI constraint is satisfied.
I don't want to drop the RI constraint. I realize that the status is
Denormalized. I want it that way. What is a good way to maintain the log?
This is the implementation:
Sql> CREATE TABLE Parent
2 (thekey int primary KEY,
3 Status Varchar2 (1),
4 effdate Date
5)
6/
Table created.
Sql> CREATE TABLE Log_table
2 (thekey int references parent (thekey),
3 Status Varchar2 (1),
4 effdate Date
5)
6/
Table created.
Sql> REM This package are used to maintain we state. We'll save the rowids of newly
Sql> REM inserted/updated rows in this package. We declare 2 arrays--one would
Sql> REM hold our new rows Rowids (newrows). The other are used to reset this array,
Sql> REM It is an ' empty ' array
Sql> Create or Replace package state_pkg
2 AS
3 Type Ridarray is Table of ROWID index by Binary_integer;
4
4 Newrows Ridarray;
5 empty Ridarray;
6 end;
7/
Package created.
Sql> REM We must set the state of the above package to some known, consistent state
Sql> REM before we being processing the row triggers. This trigger is mandatory,
Sql> REM We *cannot* rely on the ' after ' trigger to reset the ' package state. This
Sql> REM is because during a multi-row inserts or update, the row trigger may fire
Sql> REM But the after Tirgger does not have to fire--if the second row in an update
Sql> REM fails due to some constraint error--The row trigger would have fired 2 times
Sql> REM but the after trigger (which we relied on to reset the package) would never fire.
Sql> REM, would leave 2 erroneous rowids in the newrows array for the next insert/update
Sql> REM to the. Therefore, before the insert/update takes place, we ' reset '
Sql> Create or Replace trigger PARENT_BI
2 before insert or update on parent
3 begin
4 State_pkg.newrows: = State_pkg.empty;
5 end;
6/
Trigger created.
Sql> REM This trigger simply captures the rowid of the affected row and
Sql> REM saves it in the newrows array.
Sql> Create or Replace trigger Parent_aifer
2 after insert or update of the status on the parent for each row
3 begin
4 State_pkg.newrows (state_pkg.newrows.count+1): =: New.rowid;
5 end;
6/
Trigger created.
Sql> REM This trigger processes the new rows. We simply loop over the Newrows
sql> REM Array processing each newly inserted/modified the row in turn.
Sql> Create or Replace trigger Parent_ai
2 after insert or update of the status on parent
3 begin
4 for I in 1. State_pkg.newRows.count Loop
5 INSERT INTO Log_table
6 Select Thekey, status, Effdate
7 from parent where rowID = State_pkg.newrows (i);
8 End Loop;
9 End;
10/
Trigger created.
Sql> REM This demonstrates so we can process single and Multi-row inserts/updates
Sql> REM without failure (and can do it correctly)
Sql> INSERT into parent values (1, ' A ', sysdate-5);
1 row created.
Sql> INSERT into parent values (2, ' B ', sysdate-4);
1 row created.
Sql> INSERT into parent values (3, ' C ', sysdate-3);
1 row created.
Sql> INSERT into the parent select Thekey+6, status, effdate+1 from parent;
3 rows created.
Sql> select * from Log_table;
Thekey S Effdate
---------- - ---------
1 A 04-aug-99
2 B 05-aug-99
3 C 06-aug-99
7 A 05-aug-99
8 B 06-aug-99
9 C 07-aug-99
6 rows selected.
Sql> Update parent Set status = Chr (ASCII (status) +1), effdate = Sysdate;
6 rows updated.
Sql> select * from Log_table;
Thekey S Effdate
---------- - ---------
1 A 04-aug-99
2 B 05-aug-99
3 C 06-aug-99
7 A 05-aug-99
8 B 06-aug-99
9 C 07-aug-99
1 B 09-aug-99
2 C 09-aug-99
3 D 09-aug-99
7 B 09-aug-99
8 C 09-aug-99
9 D 09-aug-99
Rows selected.
Case 2-you need to access The:old values
This was a little more involved but the concept is the same. We'll save the actual old values in a array (as opposed to just the rowids of the new rows). Using tables of records this is fairly straightforward. Lets say we wanted to implement a flag delete of data--this is, instead of actually deleting the record, and you are like To set a date field to Sysdate and keep the record in the table (but hide it from queries). We need to ' undo ' the Delete.
In Oracle8.0 and up, we could use "INSTEAD of" triggers on a view to doing this, but in 7.3 the implementation would E this:
Sql> REM This is the table we are flag deleting from.
Sql> REM No One would ever access this table directly, rather,
Sql> REM They'll perform all insert/update/delete/selects against
Sql> REM A view on this table.
Sql> CREATE TABLE Delete_demo (a int,
2 b Date,
3 c VARCHAR2 (10),
4 hidden_date Date Default to_date (' 01-01-0001 ', ' dd-mm-yyyy '),
5 primary KEY (A,hidden_date))
6/
Table created.
Sql> REM this are our view. All DML would take place on the view, the table
Sql> REM is touched.
Sql> Create or replace view Delete_demo_view as
2 Select a, B, C from Delete_demo where hidden_date = to_date (' 01-01-0001 ', ' dd-mm-yyyy ')
3/
View created.
Sql> Grant all on Delete_demo_view to public
2/
Grant succeeded.
Sql> REM is the state package again. This time the array is of
Sql> REM Table%rowtype--not just a rowid
Sql> Create or Replace package delete_demo_pkg
2 AS
3 Type array is table of Delete_demo%rowtype index by Binary_integer;
4
4 oldvals Array;
5 empty array;
6 end;
7/
Package created.
Sql> REM the Reset trigger ...
Sql> Create or Replace trigger DELETE_DEMO_BD
2 before delete on Delete_demo
3 begin
4 Delete_demo_pkg.oldvals: = Delete_demo_pkg.empty;
5 end;
6/
Trigger created.
Sql> REM here, instead of capturing the ROWID, we must capture the before image
Sql> REM of the row.
Sql> REM We cannot really undo the delete here, We are just capturing the deleted
sql> REM Data
Sql> Create or Replace trigger Delete_demo_bdfer
2 before delete on Delete_demo
3 for each row
4 DECLARE
5 I number default delete_demo_pkg.oldvals.count+1;
6 begin
7 delete_demo_pkg.oldvals (i). A: =: old.a;
8 Delete_demo_pkg.oldvals (i). B: =: old.b;
9 Delete_demo_pkg.oldvals (i). c: =: OLD.C;
Ten end;
11/
Trigger created.
Sql> REM Now, we can put the deleted data back into the table. We put Sysdate
Sql> REM in as the "hidden_date field"-that shows us, the record is deleted.
Sql> Create or Replace trigger Delete_demo_ad
2 after delete on Delete_demo
3 begin
4 for I in 1. Delete_demo_pkg.oldvals.count Loop
5 INSERT into Delete_demo (A, B, C, hidden_date)
6 values
7 (Delete_demo_pkg.oldvals (i). A, Delete_demo_pkg.oldvals (i). B,
8 Delete_demo_pkg.oldvals (i). c, Sysdate);
9 End Loop;
Ten end;
11/
Trigger created.
Sql> REM now, to show it at work ...
sql> INSERT INTO Delete_demo_view values (1, sysdate, ' Hello ');
1 row created.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service