Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if a linked SQL server is running

I am writing a stored procedure which runs SELECT queries on several different linked servers using the 4-dot notation.

The problem is, if one of the linked servers is not running, the query fails with error 121 ('The semaphore timeout period has expired'). The other SELECT queries then don't run as this error stops the rest of the query executing.

I wanted to check @@ERROR then continue running the other queries.

How can I continue running the query if the connection to one of the linked servers fails?

I am using SQL 2012.

like image 210
Tim Avatar asked Mar 20 '13 16:03

Tim


People also ask

How do you check if a linked server is being used?

Use Sql Profiler Run SQL server Profiler for a month against the target server (on a spare machine) to see if the linked server login name appears, this is the login name of the connection set up in the linked server configuration.

How do I monitor a linked server in SQL Server?

You can use the flow T-SQL to get the query information, and filter the link server query from the text list.

How do I get a list of linked servers in SQL Server?

Open SQL Server Management Studio; go to Server Objects -> Linked Server. Under Linked Server node we have Providers node that already having installed provides and that mapped to SQL Server.


1 Answers

Have you tried to surround your single call with TRY-CATCH exception blocks?

     BEGIN TRY
          --First Server Connection (Server1) 192.168.1.x
          --If the connection isn't available it will raise an exception
          exec sp_testlinkedserver  @servername = Server1
          --SQL statement here
     END TRY
     BEGIN CATCH
          SELECT ERROR_MESSAGE()
     END CATCH

     BEGIN TRY
          --Second Server Connection (Server2) 192.168.2.x
          --If the connection isn't available it will raise an exception
          exec sp_testlinkedserver  @servername = Server2
          --SQL statement here
     END TRY
     BEGIN CATCH
          SELECT ERROR_MESSAGE()
     END CATCH 

sp_testlinkedserver will raise an exception inside a try block before the execution of your code but it won't stop the execution of the stored procedure.

like image 109
codingadventures Avatar answered Oct 13 '22 04:10

codingadventures