Oracle触发器表发生了变化 触发器不能读它的解决方法(必看)
更新时间:2017年04月07日 09:31:28 投稿:jingxian
下面小编就为大家带来一篇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;两句
以上这篇Oracle触发器表发生了变化 触发器不能读它的解决方法(必看)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。
相关文章
Oracle账户被锁错误:the account is locked解决方法
the account is locked意思是账户被锁定了,这种情况需要大家去解锁,这篇文章主要给大家介绍了关于Oracle账户被锁错误:the account is locked的解决方法,需要的朋友可以参考下2023-12-12
Oracle用decode函数或CASE-WHEN实现自定义排序
这篇文章主要介绍了Oracle用decode函数或CASE-WHEN实现自定义排序功能,本文通过实例代码给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下2020-05-05
oracle11g客户端连接12c服务器ORA-01017错误问题解决
这篇文章主要介绍了oracle11g客户端连接12c服务器ORA-01017错误,本文给大家分享完美解决方法,对oracle 12c错误ORA-01017问题解决方法感兴趣的朋友跟随小编一起看看吧2023-06-06
Windows Server 2012 安装oracle11g(图文教程)
这篇文章主要介绍了Windows Server 2012 安装oracle11g(图文教程),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧2019-12-12


最新评论