通常我会遇到这样的错误
EXECUTE (@STATEMENT)
SELECT @ERR_CODE = @@ERROR
它处理简单的错误,如下面的查询.. @@ERROR 返回值
insert into tab1 values(1) -- error attempt to insert unique constraint
但是下面的查询也给出了唯一约束错误,但是@ERROR没有捕获它它返回空值
insert into tab1 select id from tab2
所以上面的语句给出了唯一的约束,但 @ERROR 没有捕获它
另一个例子我有以下错误
sybase could not acquire a lock within the specified wait period
@ERROR 也没有捕获
我的问题是有没有一种方法可以捕获执行语句时的任何错误?
您绝对确定在插入约束失败和查看 @@error 之间没有任何语句吗?任何事情都会重置@@error。
事实上,您在 EXECUTE() 中使用动态 SQL - @@error 仍然可用。
尝试像我一样做下面的事情 - 你有什么不同吗?
create table tempdb..abe(a int)
select 1 a into #a
insert #a values (1)
create unique index x on tempdb..abe(a)
insert tempdb..abe select * from #a
Msg 2601, Level 14, State 2:
Server 'CRENG_QA', Line 1:
Attempt to insert duplicate key row in object 'abe' with unique index 'x'
Command has been aborted.
(0 rows affected)
select @@error
-----------
2601
execute('insert tempdb..abe select * from #a')
Msg 2601, Level 14, State 2:
Server 'CRENG_QA', Line 1:
Attempt to insert duplicate key row in object 'abe' with unique index 'x'
Command has been aborted.
(0 rows affected)
select @@error
-----------
2601
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)