30 Aralık 2011 Cuma

ORA-04091: table appuser.holidays is mutating, trigger/function may not see it

When you insert a row to table which have trigger to insert another table in remote db.
You have to care about insert statement in trigger.

for example

if you use this statement for insert to remote table


INSERT INTO appuser.holidays@otherdb SELECT * FROM holidays WHERE HOL_NAME = :New.HOL_NAME;


statement raises an error like below

ORA-04091: table appuser.holidays is mutating, trigger/function may not see it
ORA-02063: preceding line from PRODDB
ORA-02063: preceding 2 lines from OTHERDB
ORA-06512: at "APPUSER.HOLIDAYS_IUD", line 6
ORA-04088: error during execution of trigger 'APPUSER.HOLIDAYS_IUD'


to aviod tihs problem you can use table columns for inserting. like below


INSERT INTO appuser.holidays@otherdb values (:New.HOL_NAME,:New.HOL_START,:New.HOL_END,:New.HOL_TYPE);



--source of trigger


CREATE OR REPLACE TRIGGER APPUSER.HOLIDAYS_IUD
 AFTER
  INSERT OR DELETE OR UPDATE
 ON appuser.holidays
REFERENCING NEW AS NEW OLD AS OLD
 FOR EACH ROW
DECLARE
BEGIN
 IF DELETING then
     DELETE FROM appuser.holidays@otherdb WHERE HOL_NAME =:Old.HOL_NAME;
 ELSIF INSERTING then
     INSERT INTO appuser.holidays@otherdb values (:New.HOL_NAME,:New.HOL_START,:New.HOL_END,:New.HOL_TYPE);
 ELSIF UPDATING then
     DELETE FROM appuser.holidays@otherdb WHERE HOL_NAME = :Old.HOL_NAME;
     --raise error like below
     --INSERT INTO appuser.holidays@otherdb SELECT * FROM holidays WHERE HOL_NAME = :New.HOL_NAME;
     INSERT INTO appuser.holidays@otherdb values (:New.HOL_NAME,:New.HOL_START,:New.HOL_END,:New.HOL_TYPE);
 END IF;
END;
/

Hiç yorum yok:

Yorum Gönder