问题描述
我有一个与此问题类似的问题:
I've got a similar problem to this question: TRY CATCH with Linked Server in SQL Server 2005 Not Working
我正在运行此try catch:
I'm running this 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,所以我遇到了这个问题:
I don't think I can use RAISEERROR
because I'm not running my own stored procedure, I'm only running a simple select statement. I've tried using @@ERROR
but that doesn't work across a linked server either. Because the error I get is less than 20, I run into this problem:
http://msdn.microsoft.com/en-us/library /ms191515.aspx
我发现了以下问题:如何是否捕获了从链接服务器返回的错误消息?,也没有得到答复.
I found this question: How to capture error message returned from linked server? which has not been answered either.
推荐答案
我找到了解决方法,将try catch传递给链接服务器,并使用OUTPUT参数返回错误.例如:
I found out how to get around this by passing the try catch to the linked server and getting the error back using the OUTPUT parameter. For example:
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
这篇关于通过严重性为<的链接服务器捕获错误. 20的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!