Solved

How to capture timeouts in TSQL?

Posted on 2008-09-30
5
1,009 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

Free eBook: Backup on AWS

Everything you need to know about backup and disaster recovery with AWS, for FREE!

Question has a verified solution.

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

In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

737 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