SQL OPENQUERY,“ +”附近的语法不正确

时间:2019-06-03 05:06:09

标签: sql sql-server openquery

我有这个SQL查询:

DECLARE  @VAR varchar(8)
SET  @VAR = '20140202'
SELECT * FROM OPENQUERY([NMD],'SELECT * FROM NmdItemGroups01.dbo.InvTranDtl WHERE TranDate = ''' + @VAR + '''')

但是当我运行它时,会出现此错误:

Incorrect syntax near '+'

1 个答案:

答案 0 :(得分:2)

sp_executesql示例:

DECLARE  @VAR varchar(8)
SET  @VAR = '20140202'

EXEC sp_executesql 
    N'SELECT * FROM NmdItemGroups01.dbo.InvTranDtl WHERE TranDate = @TranDate', 
    N'@TranDate DATE', 
    @VAR