How to check if SQL Server exists before executing the code

Posted on 2013-09-13
Medium Priority
Last Modified: 2013-10-21
I am using the following code to dynamically create a linked server

 select @tsql =	'use [master];
					EXEC master.dbo.sp_addlinkedserver @server = N'''+@pservername+''', @srvproduct=N''sql server'';
					EXEC master.dbo.sp_serveroption @server=N'''+ @pservername+''', @optname=N''data access'', @optvalue=N''true'';
					EXEC master.dbo.sp_serveroption @server=N'''+ @pservername+''', @optname=N''rpc'', @optvalue=N''true'';
					EXEC master.dbo.sp_serveroption @server=N'''+ @pservername+''', @optname=N''rpc out'', @optvalue=N''true'';
					EXEC master.dbo.sp_serveroption @server=N'''+ @pservername+''', @optname=N''use remote collation'', @optvalue=N''true'';
					EXEC master.dbo.sp_addlinkedsrvlogin @rmtsrvname = N''' + @pservername+ ''', @locallogin = null , @useself = N''true'';'    

--print @tsql
IF @PrintFlag = 0 PRINT @tsql
ELSE   exec (@tsql)    

Open in new window

When a server does not exists I get the following error:

"OLE DB provider "SQLNCLI" for linked server "SERVER03" returned  message "Login timeout expired". "
Question by:YZlat
  • 2
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 39491458
you can use this

select * from master..sysservers where srvname = 'YourLinkedServerName'
LVL 35

Author Comment

ID: 39491483
That's not what I meant. let's say I need to create a linked server from SERVER01 to SERVER03. SERVER03 was deleted. So executing sp_addlinkedserver would give an error.

How do I avoid that?
LVL 75

Accepted Solution

Aneesh Retnakaran earned 2000 total points
ID: 39491491
from SQL server 2005 onwards you can call sp_testLinkedServer


otherwise just run a query against master database on your linked server, if it errors out, link is offline

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Microsoft Access has a limit of 255 columns in a single table; SQL Server allows tables with over 255 columns, but reading that data is not necessarily simple.  The final solution for this task involved creating a custom text parser and then reading…
This month, Experts Exchange sat down with resident SQL expert, Jim Horn, for an in-depth look into the makings of a successful career in SQL.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

624 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