我想从这个查询中尝试做的是我试图从自定义交换表中获取比指定Cutoffnumber更旧的所有行:
@Cutoffnumber INT
SELECT @SQL = 'SELECT * FROM customertransaction WHERE DATEDIFF(DD, Transactiondate, GETDATE()) > @Cutoffnumber '
但我收到以下错误,请注意,这是一个动态SQL查询。
必须声明标量变量“@Cutoffnumber”
我应该怎么做。
答案 0 :(得分:1)
答案 1 :(得分:-1)
Decalre @Cutoffnumber INT
Set @Cutoffnumber = @someValue
SELECT @SQL = 'SELECT * FROM customertransaction WHERE DATEDIFF(DD, Transactiondate, GETDATE()) >' + @Cutoffnumber
虽然看起来你可以在没有动态SQL的情况下做到这一点:
Declare @Cutoffnumber INT
Set @Cutoffnumber = @someValue
SELECT * FROM customertransaction WHERE DATEDIFF(DD, Transactiondate, GETDATE()) > @Cutoffnumber