上抛出抓执行错误给一些背景细节,比方说有一个“人”表和存储过程“PersonPerformance”返回给一个人的表现PERSONID。这两个对象都驻留在链接的sql server 2000实例上。SQL Server 2008中 - 通过一个存储过程运行链接服务器
我的目标是运行每个PERSONID的PersonPerformance proc和结果存储在驻留在SQL Server 2008实例的表。如果执行存储过程期间发生执行错误,我想说明它'失败'并发送一封电子邮件,列出存储过程完成时所有失败的PersonId。
我使用try..catch块,并检查@@ ERROR试过!= 0,但也没有工作。我假设这是因为存储过程在抛出错误后继续执行,并最终返回结果,即使它们无效。存储过程本身是一个很长的批次,没有错误处理。
下面的代码我到目前为止:
{@ReturnTable is defined here}
declare cur cursor for
select PersonId from [linked-server-name].DB.dbo.Person
open cur
declare @pId int
fetch next from cur into @pId
while (@@FETCH_STATUS = 0)
begin
begin try
print(@pId)
insert into @ReturnTable exec [linked-server-name].DB.dbo.PersonPerformance @pId
end try
begin catch
print('store person that failed here')
end catch
fetch next from cur into @pId
end
close cur
deallocate cur
我想我明白为什么它不工作,我预计我前面提到的原因(存储过程持续),但我不知道是否有办法避免这个问题,并且即使proc继续,也可以捕获执行错误。
这里的执行过程中生成的输出的快照:
101
The statement has been terminated.
Msg 2627, Level 14, State 1, Procedure OSPerfAttribution_ps, Line 626
Violation of PRIMARY KEY constraint '[email protected]__2CD2DAF1'. Cannot insert duplicate key in object '#2BDEB6B8'.
The statement has been terminated.
Msg 515, Level 16, State 2, Procedure OSPerfAttribution_ps, Line 1047
Cannot insert the value NULL into column 'BasketBenchmarkId', table '@ReturnTable'; column does not allow nulls. INSERT fails.
(3 row(s) affected)
102
(36 row(s) affected)
106
(32 row(s) affected)
该错误消息是从存储过程传播到外部SQL的输出这一事实使我相信一定会有搭上方式它。我只是很难找到我希望在这里得到一些帮助。
不得已我可能只需要保存整个输出到一个日志文件,然后搜索错误。这不是一个选项的坏处,但我仍然想知道是否有办法在执行过程中捕获错误。
这样做 - 我想这将是一个配置设置。 – Brandon 2013-03-06 19:50:06