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

所属分类: 数据库 / oracle 阅读数: 1043
收藏 0 赞 0 分享

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

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

更多精彩内容其他人还在看

oracle存储过程中return和exit区别概述及测试

至于return和exit在oracle存储过程中的应用,有些新手朋友们还是比较容易混淆的,本文将针对这两个关键字进行详细对比下,感兴趣的你可以参考下,希望可以帮助到你
收藏 0 赞 0 分享

oracle查看当前日期是第几个星期的方法

oracle查看当前日期是第几个星期方法的代码段,需要的朋友可以参考一下
收藏 0 赞 0 分享

oracle删除已存在的表的实例

查询系统表,判断表是否存在,存在则直接删除
收藏 0 赞 0 分享

oracle中文乱码解决的办法

oracle中文乱码解决的办法,需要的朋友可以参考一下
收藏 0 赞 0 分享

Oracle中在pl/sql developer修改表的2种方法

Oracle中在pl/sql developer修改表的2种方法,需要的朋友可以参考一下
收藏 0 赞 0 分享

oracle 创建表空间步骤代码

oracle 创建表空间步骤代码,需要的朋友可以参考一下
收藏 0 赞 0 分享

Oracle 查看表空间的大小及使用情况sql语句

表空间使用情况包括:查看表空间的名称及大小/查看表空间物理文件的名称及大小/查看回滚段名称及大小等等感兴趣的你可以参考下本文
收藏 0 赞 0 分享

Oracle Form中COMMIT的概述及使用技巧

针对form上面的数据变动提交到后台数据库,同时数据库提交数据,接下来将详细介绍下Form中COMMIT的使用,感兴趣的你可以参考下本文
收藏 0 赞 0 分享

Oracle跨数据库查询并插入实现原理及代码

需要从一个数据库中的表GIS_WEICHAI_DATA_1S中的数据导入到另个一数据库的表GIS_WEICHAI_DATA_1S中,接下来为你讲解跨数据库查询并插入需要的朋友可以参考下
收藏 0 赞 0 分享

Oracle 存储过程发送邮件实例学习

接下来将介绍下如何使用存储过程发送邮件这一案例实现,感兴趣的你可以参考下本文或许对你有所帮助
收藏 0 赞 0 分享
查看更多