Take the 2-minute tour ×
Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. It's 100% free, no registration required.

I am setting up a job to loop through a list of linked servers and execute a specific query against each one. I am trying to execute the query inside a TRY-CATCH block so if there's a problem with one particular server I can log it but then carry on with the other servers.

The query I'm executing inside the loop looks something like this:

BEGIN TRY
    SELECT *
    FROM OPENQUERY([server1], 'SELECT 1 AS c;');
END TRY
BEGIN CATCH
    SELECT ERROR_NUMBER(), ERROR_MESSAGE();
END CATCH;

PRINT 'We got past the Catch block!';

If there is a problem connecting to the server the code just fails immediately and doesn't transfer to the CATCH block. If the server connects but there is an error in the actual query, e.g. divide by zero, then this is caught as expected by the CATCH block.

For example, I created a linked server to a name that I know doesn't exist. When executing the above I just get:

OLE DB provider "SQLNCLI" for linked server "nonserver" returned message 
    "Login timeout expired".
OLE DB provider "SQLNCLI" for linked server "nonserver" returned message 
    "An error has occurred while establishing a connection to the server. 
    When connecting to SQL Server 2005, this failure may be caused by the 
    fact that under the default settings SQL Server does not allow remote
    connections.".
Msg 53, Level 16, State 1, Line 0
Named Pipes Provider: Could not open a connection to SQL Server [53].

I've read BOL on TRY-CATCH and know that it won't catch level 20+ errors that break the connection but this doesn't seem to be the case (this is only level 16).

Does anyone know why these errors aren't caught correctly?

share|improve this question
add comment

3 Answers 3

One thing you can try is to use sp_testlinkedserver. You can also issue the OPENQUERY using dynamic SQL (as Max correctly pointed out), to defer the parser validating the server name until runtime.

BEGIN TRY
    EXEC sp_testlinkedserver N'server1';

    EXEC sp_executesql N'SELECT * FROM OPENQUERY([server1], 
      ''SELECT 1 AS c;'');';
END TRY
BEGIN CATCH
    SELECT ERROR_NUMBER(), ERROR_MESSAGE();
END CATCH;

PRINT 'We got past the Catch block!';

While this works equally well without sp_testlinkedserver, that procedure can still be useful in preventing you from trying a whole bunch of code against that server...

share|improve this answer
2  
Thanks Aaron, didn't know about sp_testlinkedserver –  JamesLean Mar 7 '13 at 15:48
add comment

Have you tried something like this?

BEGIN TRY
    DECLARE @cmd nvarchar(max);
    SET @cmd = 'SELECT * FROM OPENQUERY([server1], ''SELECT 1 AS c;'');';
    EXEC sp_executesql @cmd;
END TRY
BEGIN CATCH
    SELECT ERROR_NUMBER(), ERROR_MESSAGE();
END CATCH;

As per the comments below, this works since the error is no longer generated at compile-time. The error now occurs at runtime, inside the sp_executesql stored procedure.

share|improve this answer
    
Thanks Max. Yes, I had thought of dynamic SQL (and the above does indeed work correctly). I am interested in WHY the error isn't caught though? –  JamesLean Mar 7 '13 at 15:40
    
@AaronBertrand If you add a simple PRINT 'Start'; at the very top of the script, this gets printed in the output, even though the connection then fails and the script exits with the error. So this would indicate a runtime error wouldn't it? Unless I'm misunderstanding it? –  JamesLean Mar 7 '13 at 15:52
    
Gah, when I added the PRINT I still had the sp_testlinkedserver call in the script. In actual fact, it doesn't get printed using my original (failing) script. So it looks like this is actually a compile-time error, which is why it doesn't get caught. –  JamesLean Mar 7 '13 at 16:10
    
@JamesLean too funny, when I went to repro to confirm what you were suggesting, I had commented out the sp_testlinkedserver call, but left the SELECT as dynamic SQL. The PRINT does not occur if you reference the server name directly, so as I had suggested earlier, BEGIN TRY is never entered because the error is raised first. –  Aaron Bertrand Mar 7 '13 at 16:18
add comment

After investigation it looks like this error isn't being caught as it is a compile-time error rather than a runtime error. To demonstrate this, try the following:

PRINT 'Before TRY';

BEGIN TRY
    SELECT 1/0;

    SELECT *
    FROM OPENQUERY([nonserver], 'SELECT 1 AS c;');
END TRY
BEGIN CATCH
    SELECT ERROR_NUMBER(), ERROR_MESSAGE();
END CATCH;

The initial PRINT statement doesn't get output, nor does the divide by zero error get executed/caught. The non-existent server causes the script to fail immediately.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.