question

David Wimbush avatar image
David Wimbush asked

Test whether linked server is up

Hi guys,

Here's an intriguing one:

I have a stored proc I use in a report to keep an eye on the SQL Agent jobs on all our servers. It goes through a list of linked servers and inserts stuff from that server's job history into a temporary table. I've wrapped each one in a TRY...CATCH and put a bit in the CATCH block to insert a message that the server wasn't available but this doesn't work if the server is not there. The CATCH doesn't happen and the proc fails with a 'Login timeout expired' error.

I've tried using sp_testlinkedserver and that works nicely with the TRY...CATCH on its own but as soon as I include anything to do with the linked server, I'm back to square one.

This code works fine and selects the message 'Login timeout expired' as a resultset:

declare @ServerUp int;
begin try
    exec @ServerUp = sp_testlinkedserver brokenlinkedserver;
    select @ServerUp as ServerUp;
end try
begin catch
    select error_message() as ErrMsg;
end catch

This code fails with the same message as an error:

declare @ServerUp int;
begin try
    exec @ServerUp = sp_testlinkedserver brokenlinkedserver;
    select  'OK' as OKMsg
    from    brokenlinkedserver.msdb.dbo.sysjobhistory;
end try
begin catch
    select  error_message() as ErrMsg;
end catch

It seems that the select from the linked server is causing the batch to error even though it shouldn't have got to that line.

So can you please either help me fix this code or suggest another way I can test whether the linked server is available?

Thanks.

sql-server-2005t-sql
2 comments
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Fatherjack avatar image Fatherjack ♦♦ commented ·
Is it possible that the whole script is being parsed and that is where the process is hanging?
0 Likes 0 ·
David Wimbush avatar image David Wimbush commented ·
I think that's it. It's a shame it doesn't just check that there is a linked server with that name and have a go at executing it. I'm guessing it needs to read metadata from the linked server's schema to build a query plan.
0 Likes 0 ·
dave 4 avatar image
dave 4 answered

Use dynamic sql to delay parsing until run time?

10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Jack Corbett avatar image
Jack Corbett answered

I did a quick test on 2008 and it seems that the error when including the select from the linked server is occurring at parse and compile time. I used the debugging features in 2008 and I never even entered the code block before I got an error when I included the select from a linked server.

1 comment
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

David Wimbush avatar image David Wimbush commented ·
Thanks, Jack. That confirms what I suspected.
0 Likes 0 ·
ReneTrevi avatar image
ReneTrevi answered
Hello, is there any simmilar function in SQLServer 7 or 2000?
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.