Solved

How to capture timeouts in TSQL?

Posted on 2008-09-30
5
989 Views
Last Modified: 2009-05-06
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
Comment
Question by:PMH4514
  • 3
  • 2
5 Comments
 
LVL 11

Expert Comment

by:aaronakin
ID: 22606800
Try using the sp_testlinkedserver procedure.  Here's the documentation on it.

http://msdn.microsoft.com/en-us/library/ms189809(SQL.90).aspx
0
 
LVL 11

Accepted Solution

by:
aaronakin earned 125 total points
ID: 22606874
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
 

Author Comment

by:PMH4514
ID: 22607967
Thanks, perfect!
0
 

Author Comment

by:PMH4514
ID: 22608505
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
 

Author Comment

by:PMH4514
ID: 22608960
oh nevermind that followup, I found answers elsewhere, need to ping the server instead. thanks again!
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question