If Linked Server is down What will happen to the Query in SQL Server?

1k views Asked by At

I understand that in MS SQL Server, if the physical server is down, then the database instance of that service will be not reachable and query shall not be able to execute from this server.

If I have a database stored procedure to fetch data from Linked Servers using IF ELSE condition, and IF one of the Linked Server is down, will the query fetch data as 0 and go to ELSE condition to fetch data from Linked Server 2?

@CSQL = 'SELECT @ROWCOUNT = COUNT(*) from [LinkedServer1].DATABASE.DBO.TABLE'
EXEC (@CSQL);

IF @ROWCOUNT>0
BEGIN

SET @SQL = 'INSERT INTO TABLE SELECT * FROM [LINKEDSEVER1].DATABASE.DBO.TABLE';
EXEC(@SQL);

END
ELSE
BEGIN

SET @SQL = 'INSERT INTO TABLE SELECT * FROM [LINKEDSEVER2].DATABASE.DBO.TABLE';
EXEC(@SQL);

END

Here If [LinkedServer1] is down, will the query fetch data from [LinkedServer1] using else condition or due to the first query check @CSQL = 'SELECT @ROWCOUNT = COUNT(*) from [LinkedServer1].DATABASE.DBO.TABLE' the query will not return any record as the LinkedServer1 is not reachable?

1

There are 1 answers

0
SixtusTyrannicus On

I get the idea but it would probably be better to use the answer given here as a starting point: Check if a linked SQL server is running

They wanted code to run on both linked servers no matter what but this is only slightly different. You can check both servers to see if they are up, assign variables in the catch blocks.

After they've run you'll know if 1 or both servers are up by checking the variables assigned, and then run your code (or not if both are down).