是否可以在变量中分配从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
对于存储过程,您还可以使用
output
参数或返回码。后者只能返回一个整数,通常首选返回错误代码而不是数据。两种技术都在下面演示。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
关于sql-server-2008 - 设置@var = exec存储过程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5439005/