Solved

How to capture timeouts in TSQL?

Posted on 2008-09-30
5
1,014 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

707 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