Bum*_*Bee 13 exec sql-server-2008
是否可以在变量中为exec存储过程返回一个值?
就像是
DECLARE @count int
SET @count = Execute dbo.usp_GetCount @Id=123
Run Code Online (Sandbox Code Playgroud)
Mar*_*ith 25
您可以使用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
Run Code Online (Sandbox Code Playgroud)
因为exec
我只知道如何使用表变量
declare @out table
(
out int
)
insert into @out
exec('select 10')
select *
from @out
Run Code Online (Sandbox Code Playgroud)
对于存储过程,您还可以使用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
Run Code Online (Sandbox Code Playgroud)
gbn*_*gbn 23
如果你在proc中使用RETURN
DECLARE @count int
EXECUTE @count = dbo.usp_GetCount @Id=123
Run Code Online (Sandbox Code Playgroud)
OUTPUT参数
DECLARE @count int
EXECUTE dbo.usp_GetCount @Id=123, @count OUTPUT
Run Code Online (Sandbox Code Playgroud)
将结果重定向到临时表/表变量
DECLARE @count int
DECLARE @cache TABLE (CountCol int NOT NULL)
INSERT @cache EXECUTE dbo.usp_GetCount @Id=123
SELECT @count = CountCol FROM @cache
Run Code Online (Sandbox Code Playgroud)
您不能将存储过程中的记录集直接分配给标量变量
Mar*_*ley 10
像往常一样,有很多方法可以做到这一点但最简单的方法是
DECLARE @count int
Execute @count = dbo.usp_GetCount @Id=123
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
84576 次 |
最近记录: |