SQL 服务器 TRY...CATCH 未捕获错误
SQL Server TRY...CATCH Is Not Catching An Error
BEGIN TRY
EXEC N'EXEC sp_testlinkedserver N''[MyLinkedServer]'';';
END TRY
BEGIN CATCH
SELECT 'LinkedServerDown' AS Result
RETURN
END CATCH
SELECT TOP(1) FirstName FROM [MyLinkedServer].TestDatabase.dbo.Customer
我第一次在 SQL 服务器中使用 TRY...CATCH 的经历到目前为止并没有给我留下深刻印象。
我已经停止了链接服务器上的 SQL 服务,以尝试测试我们的链接服务器出现故障、无法访问等情况。
这段代码没有捕获任何错误,而是抛出 "Login timeout expired" 和 "network-related or instance-specific error has occurred..." 错误并停止执行其余代码。
我的 SQL TRY...CATCH 块是否设置不正确?
根据 MSDN,sp_testlinkedserver 所做的是
Tests the connection to a linked server. If the test is unsuccessful
the procedure raises an exception with the reason of the failure.
因此,当您编译代码 (SP) 时,sp_testlinkedserver
会检查连接。但是您可以延迟它并使用动态 SQL.
捕获它
像这样 -
BEGIN TRY
EXEC sp_executesql N'EXEC sp_testlinkedserver [192.168.51.81];';
END TRY
BEGIN CATCH
SELECT 'LinkedServerDown' AS Result
END CATCH
来自MSDN
Errors Unaffected by a TRY…CATCH Construct
TRY…CATCH constructs do not trap the following conditions:
- Warnings or informational messages that have a severity of 10 or
lower.
- Errors that have a severity of 20 or higher that stop the SQL Server
Database Engine task processing for the session. If an error occurs
that has severity of 20 or higher and the database connection is not
disrupted, TRY…CATCH will handle the error.
- Attentions, such as client-interrupt requests or broken client
connections.
- When the session is ended by a system administrator by using the
KILL statement.
The following types of errors are not handled by a CATCH block when
they occur at the same level of execution as the TRY…CATCH construct:
- Compile errors, such as syntax errors, that prevent a batch from
running.
- Errors that occur during statement-level recompilation, such as
object name resolution errors that occur after compilation because
of deferred name resolution.
您需要创建您的 end testlinkedserver 存储过程。这也将捕获登录超时错误。
exec dbo.USP_testlinkedserver 'myServerNameHere'
定义如下:
CREATE PROCEDURE USP_testlinkedserver
@ServerName sysname
AS
BEGIN
SET NOCOUNT ON;
DECLARE @statement NVARCHAR(MAX), @errorMessage NVARCHAR(MAX)
SET @statement = N'SELECT * FROM OPENQUERY('+QUOTENAME(@ServerName,'[')+', ''SELECT 1'')'
BEGIN TRY
-- run the query
EXEC sp_executesql @stmt = @statement;
END TRY
BEGIN CATCH
-- show custom message
SET @errorMessage=QUOTENAME(@ServerName,'[') + ' linked server is not available. ' + ERROR_MESSAGE()
Raiserror(@errorMessage,16,1)
END CATCH;
END
BEGIN TRY
EXEC N'EXEC sp_testlinkedserver N''[MyLinkedServer]'';';
END TRY
BEGIN CATCH
SELECT 'LinkedServerDown' AS Result
RETURN
END CATCH
SELECT TOP(1) FirstName FROM [MyLinkedServer].TestDatabase.dbo.Customer
我第一次在 SQL 服务器中使用 TRY...CATCH 的经历到目前为止并没有给我留下深刻印象。
我已经停止了链接服务器上的 SQL 服务,以尝试测试我们的链接服务器出现故障、无法访问等情况。
这段代码没有捕获任何错误,而是抛出 "Login timeout expired" 和 "network-related or instance-specific error has occurred..." 错误并停止执行其余代码。
我的 SQL TRY...CATCH 块是否设置不正确?
根据 MSDN,sp_testlinkedserver 所做的是
Tests the connection to a linked server. If the test is unsuccessful the procedure raises an exception with the reason of the failure.
因此,当您编译代码 (SP) 时,sp_testlinkedserver
会检查连接。但是您可以延迟它并使用动态 SQL.
像这样 -
BEGIN TRY
EXEC sp_executesql N'EXEC sp_testlinkedserver [192.168.51.81];';
END TRY
BEGIN CATCH
SELECT 'LinkedServerDown' AS Result
END CATCH
来自MSDN
Errors Unaffected by a TRY…CATCH Construct
TRY…CATCH constructs do not trap the following conditions:
- Warnings or informational messages that have a severity of 10 or lower.
- Errors that have a severity of 20 or higher that stop the SQL Server Database Engine task processing for the session. If an error occurs that has severity of 20 or higher and the database connection is not disrupted, TRY…CATCH will handle the error.
- Attentions, such as client-interrupt requests or broken client connections.
- When the session is ended by a system administrator by using the KILL statement.
The following types of errors are not handled by a CATCH block when they occur at the same level of execution as the TRY…CATCH construct:
- Compile errors, such as syntax errors, that prevent a batch from running.
- Errors that occur during statement-level recompilation, such as object name resolution errors that occur after compilation because of deferred name resolution.
您需要创建您的 end testlinkedserver 存储过程。这也将捕获登录超时错误。
exec dbo.USP_testlinkedserver 'myServerNameHere'
定义如下:
CREATE PROCEDURE USP_testlinkedserver
@ServerName sysname
AS
BEGIN
SET NOCOUNT ON;
DECLARE @statement NVARCHAR(MAX), @errorMessage NVARCHAR(MAX)
SET @statement = N'SELECT * FROM OPENQUERY('+QUOTENAME(@ServerName,'[')+', ''SELECT 1'')'
BEGIN TRY
-- run the query
EXEC sp_executesql @stmt = @statement;
END TRY
BEGIN CATCH
-- show custom message
SET @errorMessage=QUOTENAME(@ServerName,'[') + ' linked server is not available. ' + ERROR_MESSAGE()
Raiserror(@errorMessage,16,1)
END CATCH;
END