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

前端之家收集整理的这篇文章主要介绍了sql-server-2005 – 返回两个输出参数sp_executesql前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有动态查询我想从中获取两个输出参数我使用以下代码,但输出参数返回null
  1. declare @query nvarchar(max);
  2. declare @result int;
  3. declare @type int
  4. declare @mainVarcharLength int;
  5.  
  6. set @query = 'select count(*),Type_Code from Customers WHERE Customers.Cust_Name = ''CUSTOMER 99'' '
  7. set @query = @query + ' and Cus_Is_Active = 1 Group BY Type_Code';
  8. select @query
  9.  
  10. EXEC sp_executesql @query,N'@result int OUTPUT,@type int OUTPUT',@result,@type
  11.  
  12. select @result
  13. select @type

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

解决方法

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

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

然后使用OUTPUT装饰输出;

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

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

猜你在找的MsSQL相关文章