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

相关文章

SELECT a.*,b.dp_name,c.pa_name,fm_name=(CASE WHEN a.fm_n...
if not exists(select name from syscolumns where name=&am...
select a.*,pano=a.pa_no,b.pa_name,f.dp_name,e.fw_state_n...
要在 SQL Server 2019 中设置定时自动重启,可以使用 Window...
您收到的错误消息表明数据库 'EastRiver' 的...
首先我需要查询出需要使用SQL Server Profiler跟踪的数据库标...