Sql server 捕获流中的选择性异常
失败时的某些操作会产生多个异常。考虑以下事项:Sql server 捕获流中的选择性异常,sql-server,try-catch,Sql Server,Try Catch,失败时的某些操作会产生多个异常。考虑以下事项: use tempdb go create table x (i int, s char(32)) go create table y (a int, xi int) go alter table y add constraint y_xi foreign key (xi) references x(i) go 由此产生: Msg 1776,16级,状态0,第1行没有初级或候选 引用表“x”中与引用列匹配的键 在外键“y_xi”中列出 Msg 17
use tempdb
go
create table x (i int, s char(32))
go
create table y (a int, xi int)
go
alter table y add constraint y_xi foreign key (xi) references x(i)
go
由此产生:
Msg 1776,16级,状态0,第1行没有初级或候选
引用表“x”中与引用列匹配的键
在外键“y_xi”中列出
Msg 1750,第16级,第0状态,第1行
无法创建约束。请参阅前面的错误
如果要围绕此操作包装try/catch,则只能拾取最后一个异常,而第一个异常实际上更令人感兴趣:
begin try
alter table y add constraint y_xi foreign key (xi) references x(i)
end try
begin catch
print error_number()
end catch
这就产生了1750——这告诉我约束不能被创建,但不是为什么。1776可能会有用得多,但我不知道怎么取
有人吗
TIA-e 所以答案是这是不可能的。显然,SQL Server 2012以某种方式解决了这一问题,如本文所述:
感谢@user3973227指出这一点 我相信这个问题是这个问题的翻版: