SQL Server 附近的语法不正确
Posted
技术标签:
【中文标题】SQL Server 附近的语法不正确【英文标题】:SQL Server Incorrect syntax near 【发布时间】:2018-03-26 05:52:15 【问题描述】:我有以下在 SQL Server Management Studio 中运行的 T-SQL 代码。
DECLARE JF_PF CURSOR FOR
SELECT a.Region, ..... (shorten for display) ;
OPEN JF_PF;
FETCH NEXT FROM JF_PF
INTO @varRegion, @varLocationId, @varTransDate, @varcontractTypeName, @varOriginalPrepaidDues, @varNewPrepaidDues, @varJoiningFee, @varPrepaidFeePackage;
WHILE @@FETCH_STATUS = 0
BEGIN
IF @varcontractTypeName = 'New Join' and @varNewPrepaidDues = 'Dues'
@varNewJoin_Dues_JF = @varJoiningFee;
IF @varcontractTypeName = 'New Join' and @varNewPrepaidDues = 'Prepaid'
BEGIN
@varNewJoin_Prepaid_JF = @varJoiningFee;
@varNewJoin_Prepaid_PF = @varPrepaidFeePackage;
END;
IF @varcontractTypeName = 'Renew' and @varOriginalPrepaidDues = 'Prepaid' and @varNewPrepaidDues = 'Prepaid'
BEGIN
@varRenew_PrepaidPrepaid_JF = @varJoiningFee;
@varRenew_PrepaidPrepaid_PF = @varPrepaidFeePackage;
END;
IF @varcontractTypeName = 'Upgrade' and @varOriginalPrepaidDues = 'Dues' and @varNewPrepaidDues = 'Prepaid'
BEGIN
@varUpgrade_DuesPrepaid_JF = @varJoiningFee;
@varUpgrade_DuesPrepaid_PF = @varPrepaidFeePackage;
END;
IF @varcontractTypeName = 'Upgrade' and @varOriginalPrepaidDues = 'Prepaid' and @varNewPrepaidDues = 'Prepaid'
@varUpgrade_PrepaidPrepaid_PF = @varPrepaidFeePackage;
FETCH NEXT FROM JF_PF
INTO @varRegion, @varLocationId, @varTransDate, @varcontractTypeName, @varOriginalPrepaidDues, @varNewPrepaidDues, @varJoiningFee, @varPrepaidFeePackage;
END;
CLOSE JF_PF ;
DEALLOCATE JF_PF ;
当它执行时,我在游标while循环中收到以下消息。
消息 102,第 15 级,状态 1,第 105 行 '@varNewJoin_Dues_JF' 附近的语法不正确。
消息 102,第 15 级,状态 1,第 108 行 '@varNewJoin_Prepaid_JF' 附近的语法不正确。
消息 102,第 15 级,状态 1,第 113 行 '@varRenew_PrepaidPrepaid_JF' 附近的语法不正确。
消息 102,第 15 级,状态 1,第 118 行 '@varUpgrade_DuesPrepaid_JF' 附近的语法不正确。
消息 102,第 15 级,状态 1,第 122 行 '@varUpgrade_PrepaidPrepaid_PF' 附近的语法不正确。
消息 102,第 15 级,状态 1,第 126 行 ';' 附近的语法不正确。
有人可以告诉我语法有什么问题吗?谢谢。
吉米
【问题讨论】:
显然这些变量都是之前声明过的。 您缺少关键字SET
。将其放在作业之前(例如,在 @varNewJoin_Dues_JF = @varJoiningFee;
之前)。
【参考方案1】:
在 SQL Server 中,需要使用set
或select
来设置变量。所以:
set @varNewJoin_Prepaid_JF = @varJoiningFee;
set @varNewJoin_Prepaid_PF = @varPrepaidFeePackage;
或者,使用select
将它们组合成一个语句:
select @varNewJoin_Prepaid_JF = @varJoiningFee,
@varNewJoin_Prepaid_PF = @varPrepaidFeePackage;
除了设置变量之外,您的特定代码没有做任何事情。我想知道您是否可以完全从代码中消除光标。
【讨论】:
我必须根据表数据的不同值分配不同的变量,有什么建议吗?以上是关于SQL Server 附近的语法不正确的主要内容,如果未能解决你的问题,请参考以下文章
关键字“AS”附近的语法不正确。在 SQL Server [重复]
错误 [42000] [Microsoft][SQL Native Client][SQL Server]关键字“SET”附近的语法不正确
将图像上传到 sql server 上的 varbinary 字段时,file_get_contents 值中的“附近语法不正确”