'='动态sql附近的语法不正确

时间:2015-08-18 21:23:50

标签: sql-server

我认为我的动态sql中有引号或where子句的另一个基本问题,但我似乎无法使这段代码工作。我试图从sp_executesql输出一个变量,并继续收到以下错误Msg 102, Level 15, State 1, Line 10 Incorrect syntax near '='.

所有未显示的参数都已在声明的前面声明(如果有帮助,我可以列出它们)。请帮我弄清楚我做错了什么!谢谢

DECLARE @SqlCommand2 NVARCHAR(MAX)
, @LastDate DATETIME 

SET @SqlCommand2= N'SELECT MAX('+QUOTENAME(@DateField)+')=@LDate 
                FROM '+QUOTENAME(@table)+'
                WHERE PgmAdmissionKey = @PgmAdmissionKey'

EXEC sp_executesql @SqlCommand2, N'@DateField VARCHAR(100), @table VARCHAR(100), @PgmAdmissionKey INT, @LDate DATETIME OUTPUT', @DateField=@DateField, @Table=@table, @PgmAdmissionKey=@PgmAdmissionKey, @LDate=@LastDate OUTPUT

1 个答案:

答案 0 :(得分:0)

可能意味着:

SELECT  DISTINCT(Serial_No)
FROM    Part_v_Container_Change2 AS CC
WHERE   CC.Change_Date > @Change_Start_Date AND 
        CC.Change_Date <= @Change_End_Date AND
        CC.Location = 'H Grading';