PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, April 21, 2022

[FIXED] How to test linkedserver's connectivity in TSQL

 April 21, 2022     connection, linked-server, openquery, sql, tsql     No comments   

Issue

I need to write a procedure to collect data from several remote servers,

I use linkedservers and OPENQUERY to gather data from servers, but sometimes I lose connection to some servers or I simply can't connect them (e.g. remote server is offline)- and OPENQUERY results in time-outs in these cases.

So I wanted to check linkedservers connectivity first and then if it's successful run the query, if not just move on to next the remote server.

I tried to put OPENQUERY in TRY - CATCH but it still returned me time-out error, sp_testlinkedserver procedure also returned me time-out error.

I'd really appreciate any help.


Solution

You can use below script

https://web.archive.org/web/20190201090243/https://blogs.msdn.microsoft.com/sqltips/2005/06/07/test-linked-server-connection-settings/

declare @srvr nvarchar(128), @retval int;
set @srvr = 'my_linked_srvr';
begin try
    exec @retval = sys.sp_testlinkedserver @srvr;
end try
begin catch
    set @retval = sign(@@error);
end catch;
if @retval <> 0
  raiserror('Unable to connect to server. This operation will be tried later!', 16, 2 );


Answered By - Thakur
Answer Checked By - Gilberto Lyons (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing