35 sql
您何时使用输出参数vs返回变量,反之亦然?在下面的简单示例中,我可以使用任何一个实现相同的功能.
使用输出参数
create proc dbo.TestOutput (@InValue int, @OutValue int output)
as
set @OutValue = @InValue
declare @x int
exec TestOutput @InValue = 3, @OutValue = @x output
select @x
Run Code Online (Sandbox Code Playgroud)
使用返回变量:
create proc dbo.TestReturn (@InValue int)
as
return @InValue
declare @x int
exec @x = TestReturn @InValue = 3
select @x
Run Code Online (Sandbox Code Playgroud)
如你所见,他们都做同样的事情.有人能告诉我一个例子,输出参数与返回变量的选择会有所不同吗?
Kev*_*che 17
我更喜欢:
当您只需要返回一个项目时使用返回值.
需要返回多个值时使用输出参数.
另一种常见的使用模式,虽然不是我的首选,但是仅使用返回值来通知成功或失败以及需要返回的任何内容的输出参数.
Rem*_*anu 12
这是T-SQL,而不是C.永远不要使用返回值,许多客户端API使得处理返回值变得很痛苦,如果不是不可能的话.始终使用OUTPUT参数.