declare @nopqty decimal(10,2)
declare @ntotamt decimal(10,2)
declare @npartno nvarchar(20)
SET @Orgcd =101
SET @npartno = "A 0001 150"
declare @qry nvarchar(4000)
SET @qry = 'SELECT @nopqty = oqty' + convert(varchar(3), @norgcd) +
', @ntotamt = ocost' + convert(varchar(3), @norgcd) +
' FROM stock WHERE part = @npartno'
exec sp_executesql @qry
错误
必须声明标量变量
然后我用了
exec sp_executesql @qry, N'@nopqty decimal(10,2),@ntotamt decimal(10,2),@npartno nvarchar(20)',@nopqty=@nopqty,@ntotamt=@ntotamt,@npartno=@npartno
但是当打印@nopqty时,@ntotamt为null
如何从这些@qry中取出@ nopqty,@ntotamt?
我需要这些值进行一些计算
答案 0 :(得分:2)
将参数指定为output
。例如:
declare @name varchar(10)
exec sp_executesql
N'select top 1 @name = name from sys.tables',
N'@name varchar(10) output',
@name = @name output
print @name