由于After触发器,尝试插入表时,记录插入失败

时间:2015-11-24 07:21:03

标签: sql oracle plsql

create or replace 
trigger trigger_craftShopItems 
after insert on craftshopitems for each row

declare

user_name varchar2(20);
new_craftId number;
new_craftItemName varchar2(40);
new_CraftVendor  varchar2(40);
new_CraftStoreId number;

begin

  select user into user_name from dual;

  select craftId,craftItemName,CraftVendor,CraftStoreId into new_craftId,new_craftItemName,new_CraftVendor,new_CraftStoreId
  from craftshopitems where rownum =(select max(rownum) from craftshopitems);

  dbms_output.put_line('Insert operation performed by :'||user_name);

  dbms_output.put_line('New CraftID: '|| new_craftId);

  dbms_output.put_line('New CraftItemName: '||new_craftItemName);

  dbms_output.put_line('New CraftVendor: '|| new_CraftVendor);

  dbms_output.put_line('NewCraftStoreId:'|| new_CraftStoreId);  

end trigger_craftShopItems;

***********************错误讯息*******************

SQL Error: ORA-04091: table ORA_JSHAH87.CRAFTSHOPITEMS is mutating, trigger/function may not see it
ORA-06512: at "ORA_JSHAH87.TRIGGER_CRAFTSHOPITEMS", line 13
ORA-04088: error during execution of trigger 'ORA_JSHAH87.TRIGGER_CRAFTSHOPITEMS'
04091. 00000 -  "table %s.%s is mutating, trigger/function may not see it"
*Cause:    A trigger (or a user defined plsql function that is referenced in
           this statement) attempted to look at (or modify) a table that was
           in the middle of being modified by the statement which fired it.
*Action:   Rewrite the trigger (or function) so it does not read that table.

1 个答案:

答案 0 :(得分:3)

您可以使用:NEW获取新插入的数据。

create or replace 
trigger trigger_craftShopItems 
after insert on craftshopitems for each row

declare

user_name varchar2(20);

begin

  select user into user_name from dual;

  dbms_output.put_line('Insert operation performed by :'||user_name);
  dbms_output.put_line('New CraftID: '|| :NEW.craftId);
  dbms_output.put_line('New CraftItemName: '|| :NEW.craftItemName);
  dbms_output.put_line('New CraftVendor: '|| :NEW.CraftVendor);
  dbms_output.put_line('NewCraftStoreId:'|| :NEW.CraftStoreId);  

end trigger_craftShopItems;