微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

是否可以在变量中分配从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

原文地址:https://www.jb51.cc/mssql/81618.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐