处理nvarchar参数与十进制

时间:2017-07-09 14:05:48

标签: sql-server

这有效:

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)

1 个答案:

答案 0 :(得分:3)

您查询PRODUCTGROUP的值为4而不是400,因为您忘记了nvarchar的尺寸:

exec sp_executesql @sql, N'@minAmount nvarchar(4000)', @minAmount