Вопрос

I have a case in which I have SQL code with an IF-ELSE block. The code in the IF portion should not be reached, but I still get an error when the SQL executes. In the code I first test for the linked server, and when that fails, @reval is set to 1 and the ELSE block should execute and avoid the code in the IF block that needs to query the linked server, but I get this error:

Msg -1, Level 16, State 1, Line 0 SQL Server Network Interfaces: Error Locating Server/Instance Specified [xFFFFFFFF].

I am running the query from within SSMS 2012. Why does this error occur?

declare @clientCode VARCHAR(7)
set @clientCode = '8001299'
declare @year INT
set @year = 2013

DECLARE @retVal INT        
-- test connectivity with linked database server
BEGIN TRY
       EXEC @retVal = sys.sp_testlinkedserver N'ATLAS'  
END TRY
BEGIN CATCH
       SET @retval = SIGN(@@ERROR)
END CATCH

IF @retval = 0 -- connection attempt successful 
BEGIN

--THE FOLLOWING INSERT SQL STATEMENT CAUSES THE ERROR

        SET @contIndex = (SELECT ContIndex FROM ATLAS.Engine_sp.dbo.tblEngagement WHERE ClientCode = @clientCode)          
END
ELSE -- could not connect 
BEGIN
       -- execute code to pull from linked server 
END
Это было полезно?

Решение

It'll still parse and bind everything before it executes it. It's failing to bind here.

You could use sp_executesql to execute that line, and it should only validate when sp_executesql is actually called.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top