Solved

How to capture timeouts in TSQL?

Posted on 2008-09-30
5
982 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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Join & Write a Comment

In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

705 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now