我有一个与此问题类似的问题:TRY CATCH with Linked Server in SQL Server 2005 Not Working
我正在运行这个try catch:
Declare @command nvarchar(100)
SET @command = 'SELECT column FROM table'
BEGIN TRY
BEGIN TRY
exec ' + @Server_Name + @DB_name + '.dbo.sp_executesql @command
END TRY
BEGIN CATCH
PRINT EXCEPTION
END CATCH
我认为我不能使用
RAISEERROR
,因为我没有运行自己的存储过程,而是仅运行了一个简单的select语句。我尝试使用@@ERROR
,但在链接的服务器上也无法使用。因为我得到的错误小于20,所以我遇到了这个问题:If a remote stored procedure calls RAISERROR with severity less than 20 and the remote stored procedure is scoped within a TRY block on the local server, RAISERROR does not cause control to pass to the CATCH block of the TRY…CATCH construct
http://msdn.microsoft.com/en-us/library/ms191515.aspx
我发现了这个问题:How to capture error message returned from linked server?也没有得到回答。
最佳答案
我通过将try catch传递到链接服务器并使用OUTPUT参数找回错误来找到解决方法。例如:
SET @command = '
BEGIN TRY
exec (''select * from xxx'')
SELECT @resultOUT = @@ERROR
END TRY
BEGIN CATCH
SELECT @resultOUT = @@ERROR
END CATCH'
SET @ParmDefinition = N'@resultOUT nvarchar(5) OUTPUT'
exec my_linked_server.sp_executesql
@command,
@ParmDefinition,
@resultOUT=@result OUTPUT
关于sql-server - 通过严重性<20的链接服务器捕获错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13315128/