• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1039
  • Last Modified:

How to capture timeouts in TSQL?

Hello

In a TSQL stored procedure (SQL Server 2005) I can do something like this:

      If (@@Error <> 0)
      Begin


to handle an error.

How can I trap a timeout? In my case, I am executing code against a linked server, and if the linked server is unavailable, I need to handle it, but the @@Error does not trap a timeout.

Thanks
0
PMH4514
Asked:
PMH4514
  • 3
  • 2
1 Solution
 
aaronakinCommented:
Try using the sp_testlinkedserver procedure.  Here's the documentation on it.

http://msdn.microsoft.com/en-us/library/ms189809(SQL.90).aspx
0
 
aaronakinCommented:
An example of testing the connection and catching the error would be:

BEGIN TRY
  EXEC sp_testlinkedserver N'RemoteServerName'
END TRY

BEGIN CATCH
  SELECT
    ERROR_NUMBER() AS ErrorNumber,
    ERROR_SEVERITY() AS ErrorSeverity,
    ERROR_STATE() AS ErrorState,
    ERROR_PROCEDURE() AS ErrorProcedure,
    ERROR_LINE() AS ErrorLine,
    ERROR_MESSAGE() AS ErrorMessage
END CATCH
0
 
PMH4514Author Commented:
Thanks, perfect!
0
 
PMH4514Author Commented:
A followup question if you could be so kind.

I added the call to sp_testlinkedserver  within my local stored procedure, with the try/catch as you described.

It works as expected if, for example, I supply a name that simply does not exist. I get a result set as you defined, error number 7202, error message 'Could not find server 'blah' in sys.servers. Verify that the correct server name was specified. If necessary, execute the stored procedure sp_addlinkedserver to add the server to sys.servers.'..

This is as I would expect.

However, I then tried unplugging the network cable to the actual linked server and re-ran it, after approximately 30 seconds for the timeout, I get the below error messages, but nothing is selected as output, and the error values are not inserted into the logging table.  It is as if the code contained in the catch is not being executed, rather the errors below. Any thoughts?


OLE DB provider "SQLNCLI" for linked server "SW-05" returned message "Login timeout expired".
OLE DB provider "SQLNCLI" for linked server "SW-05" 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]. 

Open in new window

0
 
PMH4514Author Commented:
oh nevermind that followup, I found answers elsewhere, need to ping the server instead. thanks again!
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now