Oracle触发器表发生了变化 触发器不能读它解决方法

出现原因,是因为在更新的的表和读取的表是同一个表。


  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;两句

  • 6
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值