sql-server-2005 – 返回两个输出参数sp_executesql

前端之家收集整理的这篇文章主要介绍了sql-server-2005 – 返回两个输出参数sp_executesql前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有动态查询我想从中获取两个输出参数我使用以下代码,但输出参数返回null
declare @query nvarchar(max);
declare @result int; 
declare @type int
declare @mainVarcharLength int; 

set @query = 'select   count(*),Type_Code from Customers   WHERE Customers.Cust_Name =  ''CUSTOMER 99''  '
set @query = @query + '  and Cus_Is_Active = 1  Group BY   Type_Code';
select  @query

EXEC sp_executesql @query,N'@result int OUTPUT,@type int OUTPUT',@result,@type

select @result
select @type

如何解决,以及如何传递多个输出参数

解决方法

您需要说明分配给输出内容;

set @query =’select @ result = count(*),@ type = Type_Code from Customers ….’

然后使用OUTPUT装饰输出;

EXEC sp_executesql @query,@result OUTPUT,@type OUTPUT

(您也可以通过“CUSTOMER 99”作为输入)

原文链接:https://www.f2er.com/mssql/75497.html

猜你在找的MsSQL相关文章