动态Sql错误,'>'附近的语法无效

时间:2018-01-15 19:29:25

标签: stored-procedures sql-server-2012

我有下面的动态sql语句,我收到“>'附近的错误语法。”错误信息。我无法弄清楚问题是什么,不确定我是否错过了勾号我试图添加额外的刻度线,似乎没有任何效果。

Declare @filters varchar(max)
SET @filters = 'Where PaymentAmount > 0'
BEGIN 
SET @filters = @filters + ' AND CONVERT(DATE, AccountingDate) >= '''+ cast @BeginDate as nvarchar) + ''''
SET @filters = @filters + ' AND CONVERT(DATE, AccountingDate) <= '''+ cast(@EndDate as nvarchar) + ''''
END

SET @SQLString = 'Select 
  ,[ReturnDate]
  ,[PolicyNumber]
From dbo.Bil_ReturnsRepository' + @filters

EXEC(@SQLString)

1 个答案:

答案 0 :(得分:3)

在将@filters连接到@SQLString之前,需要另一个空格。

SET @SQLString = 'Select 
  ,[ReturnDate]
  ,[PolicyNumber]
From dbo.Bil_ReturnsRepository ' + @filters

否则生成的sql将是

...
From dbo.Bil_ReturnsRepositoryWhere PaymentAmount > 0
...