November 30, 2011 at 8:37 am
I've got a simple procedure that queries 2 different linked servers based upon a selection of "server" from a drop-down box in an application. In the code snippet below, this is the @server parameter (in this case I've just declare it as a variable for testing purposes...
DECLARE
@Day datetime = NULL,
@server varchar(15) = 'NAS2-MSSQL1'
BEGIN
IF (@Day) IS NULL
SET @Day = CAST(DATEDIFF(dd,0 ,GETDATE()) AS DATETIME)
ELSE
SET @Day = CAST(DATEDIFF(dd,0 ,@Day) AS DATETIME)
IF RTRIM(@Server) = 'NAS2-MSSQL1'
BEGIN
SELECT top 5 *
FROM [NAS2-MSSQL1].Coreacquire.[dbo].[commonTNP] WITH(READUNCOMMITTED)
WHERE TranTime < @Day
END
IF RTRIM(@Server) = 'NAS2-CCSTGSQL'
BEGIN
SELECT top 5 * -- This is not
FROM [NAS2-CCSTGSQL].Coreacquire_STAGING.[dbo].[commonTNP] WITH(READUNCOMMITTED)
WHERE TranTime < @Day
END
END
When attempting to compile it, it throws errors about the one of the linked servers (because it's currently offline). Why does this fail when the query to the linked server is nested within an IF..ELSE block and it's not even being called?
OLE DB provider "SQLNCLI10" for linked server "NAS2-CCSTGSQL" returned message "Login timeout expired".
OLE DB provider "SQLNCLI10" for linked server "NAS2-CCSTGSQL" 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].
______________________________________________________________________________Never argue with an idiot; Theyll drag you down to their level and beat you with experience
November 30, 2011 at 9:20 am
That's just how it works. You could use dynamic SQL to avoid the issue, but make sure you use try - catch to handle any run time permission/connectivity issues.
The probability of survival is inversely proportional to the angle of arrival.
November 30, 2011 at 9:24 am
sturner (11/30/2011)
That's just how it works. You could use dynamic SQL to avoid the issue, but make sure you use try - catch to handle any run time permission/connectivity issues.
In addition to that, I noticed you are using SELECT TOP 5 * with no ORDER BY. If you are assuming some sort of order to the TOP 5, you must use ORDER BY.
Jared
Jared
CE - Microsoft
November 30, 2011 at 9:32 am
Jared, that's just sample code 😉 To prove the concept of testing something against a linked server - would never do that in working code!
______________________________________________________________________________Never argue with an idiot; Theyll drag you down to their level and beat you with experience
November 30, 2011 at 9:35 am
MyDoggieJessie (11/30/2011)
Jared, that's just sample code 😉 To prove the concept of testing something against a linked server - would never do that in working code!
Ha! I realized who posted this after I made my post. Of course you wouldn't do something like that! Carry on... 🙂
Jared
Jared
CE - Microsoft
Viewing 5 posts - 1 through 4 (of 4 total)
You must be logged in to reply to this topic. Login to reply