加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > MsSql教程 > 正文

sql-server-2008 – set @var = exec stored_procedure

发布时间:2020-12-12 16:44:23 所属栏目:MsSql教程 来源:网络整理
导读:是否可以在变量中分配从exec存储过程返回的值? 就像是 DECLARE @count intSET @count = Execute dbo.usp_GetCount @Id=123 解决方法 您可以使用sp_executesql而不是exec将其分配给标量输出参数 DECLARE @out intEXEC sp_executesql N'select @out_param=10',
是否可以在变量中分配从exec存储过程返回的值?

就像是

DECLARE @count int
SET @count = Execute dbo.usp_GetCount @Id=123

解决方法

您可以使用sp_executesql而不是exec将其分配给标量输出参数
DECLARE @out int

EXEC sp_executesql N'select @out_param=10',N'@out_param int OUTPUT',@out_param=@out OUTPUT

SELECT @out

对于exec,我只知道如何使用表变量来做

declare @out table
(
out int
)

insert into @out
exec('select 10')

select * 
from @out

对于存储过程,还可以使用输出参数或返回码.后者可以仅返回单个整数,通常优选返回错误代码而不是数据.两种技术如下所示.

create proc #foo 
@out int output
as
set @out = 100
return 99

go

declare @out int,@return int

exec @return = #foo @out output

select @return as [@return],@out as [@out]

drop proc #foo

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读