返回两个输出参数sp_executesql

AMH*_*AMH 10 sql-server-2005

我有动态查询我想从中获取两个输出参数我使用了以下代码,但输出参数返回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
Run Code Online (Sandbox Code Playgroud)

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

Ale*_* K. 15

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

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

然后用OUTPUT; 装饰输出;

EXEC sp_executesql @query,   
    N'@result int OUTPUT, @type int OUTPUT',
    @result OUTPUT, 
    @type OUTPUT
Run Code Online (Sandbox Code Playgroud)

(你也可以''CUSTOMER 99''作为输入传递)