Unusual table table1 is mutating, trigger/function may not see it error in Oracle

喜夏-厌秋 提交于 2019-12-25 09:33:41

问题


I have a trigger like this: (Basically on update of a column in table1, I update a column in table 2)

CREATE OR REPLACE TRIGGER AAA   AFTER UPDATE 
   ON TABLE_1    REFERENCING NEW AS NEWROW OLD AS OLDROW
   FOR EACH ROW
WHEN (
NEWROW.DELETED ='Y' AND NEWROW.ID IN (41,43)
AND OLDROW.DELETED = 'N'
      )
DECLARE
id_1 number;
id_2 number;
id_3 number;
BEGIN

select id_1, id_2,id_3 into id_1,id_2,id_3  from  table_1 where id_1 = :NEWROW.id1 and id2 = some_other_row.id2;
if id_1 is  null
then

 update table2 set deleted = 'Y' ,   where table2.id_1 = id_1 and table2.id_2=id_2 and table2.id_3 = id_3;

   end if;

EXCEPTION
   WHEN OTHERS
   THEN
      -- Consider logging the error and then re-raise
      RAISE;
END AAA;
/

When I update table1 I get:

ORA-04091: table table1 is mutating, trigger/function may not see it 

I thought this error happens only when you are updating the table on which the trigger is trying to update something. But here I am updating table1 and trigger is supposed to update table2. SO why is the error?


回答1:


It's the SELECT statement that is causing the problem here. Inside the trigger, you cannot SELECT from the same table. In your example, you don't need/can't use the SELECT statement. You can get the values by simply using :newrow.id_1, :newrow.id_2 and :newrow.id_3.



来源:https://stackoverflow.com/questions/16800709/unusual-table-table1-is-mutating-trigger-function-may-not-see-it-error-in-oracl

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!