这有效:
declare @sql nvarchar(4000)
declare @minAmount decimal
set @sql = N'select PRODUCTGROUP
from dbo.ICMaster
where ONHAND > @minAmount'
set @minAmount = 400
print @sql
exec sp_executesql @sql, N'@minAmount decimal', @minAmount
但是,只要我将@minAmount
更改为nvarchar
,它就不会(没有错误,但输出为空)。我错过了什么?
declare @sql nvarchar(4000)
declare @minAmount nvarchar(100)
set @sql = N'select PRODUCTGROUP
from dbo.ICMaster
where PRODUCTGROUP = @minAmount'
set @minAmount = '400'
print @sql
exec sp_executesql @sql, N'@minAmount nvarchar', @minAmount
我也尝试过:
set @minAmount = char(39)+'400'+char(39)
答案 0 :(得分:3)
您查询PRODUCTGROUP
的值为4
而不是400
,因为您忘记了nvarchar
的尺寸:
exec sp_executesql @sql, N'@minAmount nvarchar(4000)', @minAmount