Tim*_*Tim 6 sql-server sql-server-2012
我正在编写一个存储过程,它SELECT使用4点表示法在几个不同的链接服务器上运行查询.
问题是,如果其中一个链接服务器未运行,则查询将失败,并显示错误121 ('The semaphore timeout period has expired').然后其他SELECT查询不会运行,因为此错误会停止执行其余查询.
我想检查@@ERROR然后继续运行其他查询.
如果与其中一个链接服务器的连接失败,如何继续运行查询?
我正在使用SQL 2012.
您是否尝试使用TRY-CATCH异常块包围您的单个呼叫?
BEGIN TRY
--First Server Connection (Server1) 192.168.1.x
--If the connection isn't available it will raise an exception
exec sp_testlinkedserver @servername = Server1
--SQL statement here
END TRY
BEGIN CATCH
SELECT ERROR_MESSAGE()
END CATCH
BEGIN TRY
--Second Server Connection (Server2) 192.168.2.x
--If the connection isn't available it will raise an exception
exec sp_testlinkedserver @servername = Server2
--SQL statement here
END TRY
BEGIN CATCH
SELECT ERROR_MESSAGE()
END CATCH
Run Code Online (Sandbox Code Playgroud)
sp_testlinkedserver 将在执行代码之前在try块内引发异常,但它不会停止执行存储过程.
| 归档时间: |
|
| 查看次数: |
9934 次 |
| 最近记录: |