• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Oracle触发器表发生了变化 触发器不能读它的解决方法(必看)

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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

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触发器表发生了变化 触发器不能读它的解决方法(必看)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持极客世界。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
MySQL数据库重命名的快速且安全方法(3种)发布时间:2022-02-08
下一篇:
浅析MongoDB 全文检索发布时间:2022-02-08
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap