SQL IF块代码即使不执行也会导致错误

编程入门 行业动态 更新时间:2024-10-11 21:24:19
本文介绍了SQL IF块代码即使不执行也会导致错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个带有IF-ELSE块的SQL代码. IF部分中的代码不应该到达,但是在执行SQL时仍然出现错误.在我首先测试链接服务器的代码中,当失败时,@reval设置为1,并且ELSE块应执行并避免IF块中的代码需要查询链接服务器,但是我收到此错误:

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].

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

我正在从SSMS 2012中运行查询. 为什么会发生此错误?

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

-以下插入SQL语句导致错误

--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.

您可以使用sp_executesql执行该行,并且它仅应在实际调用sp_executesql时进行验证.

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

更多推荐

SQL IF块代码即使不执行也会导致错误

本文发布于:2023-11-23 13:28:56,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1621615.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:也会   错误   代码   SQL

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!