sybase替代方式为@@ error捕获错误

时间:2016-01-25 14:58:24

标签: sybase

实际上我正在捕捉这样的错误

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没有抓到

我的问题是有哪种方法可以捕获执行语句的任何错误?

1 个答案:

答案 0 :(得分:0)

您是否完全确定在插入约束失败和查看@@错误之间没有声明?任何事情都会重置@@ 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
相关问题