I'm in the process of creating a view but before doing so I'm testing and working with multiple linked servers returning column values in a select union statement, I use the following simple code:
SELECT * FROM OPENQUERY ([linkedserver1],'SELECT TOP 1 [Column1], [Column2] FROM [db].[table]')
UNION ALL
SELECT * FROM OPENQUERY ([linkedserver2],'SELECT TOP 1 [Column1], [Column2] FROM [db].[table]')
UNION ALL
SELECT * FROM OPENQUERY ([linkedserver3],'SELECT TOP 1 [Column1], [Column2] FROM [db].[table]')
It returns the following results as intended:
|Column1|Column2|
| 001 | Pass |
| 010 | Pass |
| 100 | Pass |
But my problem comes in that when one of the linked servers go offline example [linkedserver2], no results get shown and I only get:
OLE DB provider "SQLNCLI11" for linked server "[linkedserver2]" returned
message "Login timeout expired".
OLE DB provider "SQLNCLI11" for linked server "[linkedserver2]" returned
message "A network-related or instance-specific error has occurred while
establishing a connection to SQL Server. Server is not found or not
accessible. Check if instance name is correct and if SQL Server is
configured to allow remote connections. For more information see SQL
Server Books Online.".
Msg 53, Level 16, State 1, Line 0
Named Pipes Provider: Could not open a connection to SQL Server [53].
The error is obvious, but what I'd like to know is if I can put anything in my query that will continue to execute and return results for the other two linked servers and just skip [linkedserver2] as shown below:
|Column1|Column2|
| 001 | Pass |
| 100 | Pass |
I was thinking about using Try...Catch but I'm not a SQL guru to that extent yet. Any help would be appreciated!
Thanks.