出现原因,是因为在更新的的表和读取的表是同一个表。
create or replace trigger t_userupdatet before update on t_user referencing old as old new as n_row for each row declare u_xtfidemp1 varchar(36); u_xtempcode1 varchar(20); u_xtempcodecount int:=0; u_xtfidempcount int:=0; u_id1 int:=0; begin u_xtfidemp1:=:n_row.u_xtfidemp; u_xtempcode1:=:n_row.u_xtempcode; u_id1:=:n_row.u_id; select count(u_xtempcode) into u_xtempcodecount from eas.t_user where u_xtempcode is not null and u_xtempcode=u_xtempcode1 and u_id<>u_id1; select count(u_xtfidemp) into u_xtfidempcount from eas.t_user where u_xtfidemp is not null and u_xtfidemp=u_xtfidemp1 and u_id<>u_id1; if u_xtempcodecount>0 or u_xtfidempcount>0 then raise_application_error(-20001, 'eas.t_user u_xtempcode,u_xtfidemp,u_gzcode更新数据时有错误,有重复'); end if; end;
出现错误时,是因为触发器在t_userupdatet在t_user上,触发器内部有读取了t_user所以有错误。
修改如下
create or replace trigger t_userupdatet before update on t_user referencing old as old new as n_row for each row declare u_xtfidemp1 varchar(36); u_xtempcode1 varchar(20); u_xtempcodecount int:=0; u_xtfidempcount int:=0; u_id1 int:=0; pragma autonomous_transaction; begin u_xtfidemp1:=:n_row.u_xtfidemp; u_xtempcode1:=:n_row.u_xtempcode; u_id1:=:n_row.u_id; select count(u_xtempcode) into u_xtempcodecount from eas.t_user where u_xtempcode is not null and u_xtempcode=u_xtempcode1 and u_id<>u_id1; select count(u_xtfidemp) into u_xtfidempcount from eas.t_user where u_xtfidemp is not null and u_xtfidemp=u_xtfidemp1 and u_id<>u_id1; if u_xtempcodecount>0 or u_xtfidempcount>0 then raise_application_error(-20001, 'eas.t_user u_xtempcode,u_xtfidemp,u_gzcode更新数据时有错误,有重复'); end if; commit; end;
多了pragma autonomous_transaction;commit;两句
以上这篇oracle触发器表发生了变化 触发器不能读它的解决方法(必看)就是www.887551.com分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持www.887551.com。