SQL 存储过程,何时使用输出参数与返回变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3815502/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 07:42:28  来源:igfitidea点击:

Stored Procedure, when to use Output parameter vs Return variable

sql

提问by

When would you use an output parameter vs a return variable, or vice versa? In the following simple example, I can achieve the same thing using either one.

什么时候使用输出参数和返回变量,反之亦然?在下面的简单示例中,我可以使用任何一个来实现相同的目的。

Using output parameter

使用输出参数

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 

Using return variable:

使用返回变量:

create proc dbo.TestReturn (@InValue int)
as
return @InValue

declare @x int
exec @x = TestReturn @InValue = 3
select @x 

As you can see, they both do the same thing. Can someone show me an example where the choice of a output parameter vs a return variable would make a difference?

如您所见,它们都做同样的事情。有人可以告诉我一个示例,其中输出参数与返回变量的选择会有所不同吗?

回答by Kevin LaBranche

I prefer:

我更喜欢:

Using a return value when you only need to return one item.

当您只需要返回一项时使用返回值。

Using output parameters when you need to return more than one value.

当您需要返回多个值时使用输出参数。

Another common usage pattern, although not my preference, is to use return values only to inform of success or failure and output parameters for anything that needs to be returned.

另一种常见的使用模式(虽然不是我的偏好)是仅使用返回值来通知成功或失败以及需要返回的任何内容的输出参数。

回答by Remus Rusanu

This is T-SQL, not C. Never use return values, many client side APIs make dealing with return values a pain if not plain impossible. Always use OUTPUT parameters.

这是 T-SQL,而不是 C。永远不要使用返回值,许多客户端 API 使处理返回值变得很痛苦,如果不是完全不可能的话。始终使用 OUTPUT 参数。

回答by Mark Brackett

Since return values only work with int, it ends up being "inconsistent". I prefer the output param for consistency.

由于返回值仅适用于int,因此最终会“不一致”。我更喜欢输出参数的一致性。

Also, output params force the caller to recognize the returned value. IME, return values are routinely ignored.

此外,输出参数强制调用者识别返回值。IME,返回值通常会被忽略。

回答by Jeff Wight

You should use RETURN to return a value from a procedure much in the same way you'd use EXIT to return a value in a batch script. Return isn't really for parameter passing, but rather as a way to quit out of a procedure or query. As per MSDN documentation:

您应该使用 RETURN 从过程中返回一个值,就像使用 EXIT 在批处理脚本中返回一个值一样。Return 并不是真正用于参数传递,而是作为退出过程或查询的一种方式。根据MSDN 文档

Unless documented otherwise, all system stored procedures return a value of 0. This indicates success and a nonzero value indicates failure.

除非另有说明,否则所有系统存储过程都返回值 0。这表示成功,非零值表示失败。

This becomes more evident once you recognize the lack of any ability to define a type to your return value. It has to be INT.

一旦您认识到缺乏为返回值定义类型的任何能力,这一点就会变得更加明显。它必须是INT。

回答by NoDisplay

I will answer this question in different ways:

我会以不同的方式回答这个问题:

If you would like to return one value you have both the options. But if you would like to return multiple values you only need to stick with output parameters.

如果您想返回一个值,您有两种选择。但是如果你想返回多个值,你只需要坚持使用输出参数。

Second Scenario: In C# you have the control of type if you are using output parameters.

第二种情况:在 C# 中,如果使用输出参数,则可以控制类型。

Third scenario: Function vs. Procedure select the one suiting to your needs.

第三种场景:功能 vs. 过程选择适合您需求的一种。

Hope this helps

希望这可以帮助

回答by jgritten

taken from here

取自这里

  • When you want to return one or more items with a data type then it is better to use an output parameter.
  • Generally, use an output parameter for anything that needs to be returned.
  • When you want to return only one item with only an integer data type then it is better to use a return value.
  • Generally, the return value is only to inform success or failure of the Stored Procedure.
  • A return List item a value of 0 indicates success and any non-zero value indicates failure.
  • 当您想返回一个或多个具有数据类型的项目时,最好使用输出参数。
  • 通常,对需要返回的任何内容使用输出参数。
  • 当您只想返回一个只有整数数据类型的项目时,最好使用返回值。
  • 通常,返回值只是通知存储过程成功或失败。
  • 返回值 0 的列表项表示成功,任何非零值表示失败。

回答by user3423986

I use return value to many things because it is more performative such as:

我将返回值用于很多事情,因为它更具执行力,例如:

1 - When you insert or change an item when I do the validation in the database Return positive number to return the Identity and negative with the number of the error, it is faster.

1 - 当我在数据库中进行验证时插入或更改项目时返回正数以返回标识,负数返回错误编号,速度更快。

2- When I make an appointment with paging use it to return the total amount of records is also faster and less costly. For the rest I use Output when there are several returns or different types of int. And of course I use recorset to return a list of items

2-当我预约分页时,使用它来返回记录总量也更快,成本更低。对于其余部分,当有多个返回或不同类型的 int 时,我使用 Output。当然,我使用 recorset 返回项目列表

How do I use the Dapper to make my queries I do not suffer.

我如何使用 Dapper 进行查询,我不会受到影响。