Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从存储过程中的exec sp_executesql获取值_C#_Sql Server_Sql Server 2005 - Fatal编程技术网

C# 从存储过程中的exec sp_executesql获取值

C# 从存储过程中的exec sp_executesql获取值,c#,sql-server,sql-server-2005,C#,Sql Server,Sql Server 2005,错误 必须声明标量变量 然后我用 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) +

错误

必须声明标量变量

然后我用

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
但是当打印@nopqty,@ntotamt时,它是空的

如何从这些@qry中取出@nopqty、@ntotamt


我需要这些值进行一些计算

将参数指定为
输出
。例如:

exec sp_executesql @qry, N'@nopqty decimal(10,2),@ntotamt decimal(10,2),@npartno nvarchar(20)',@nopqty=@nopqty,@ntotamt=@ntotamt,@npartno=@npartno

@Orgcd未声明,并且不能使用双引号指定varchar,请使用撇号。除此之外,你能给我们看一下@qry吗?我忘了写那行声明为int
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