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

时间:2022-09-10 23:22:35

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.

您何时使用输出参数vs返回变量,反之亦然?在下面的简单示例中,我可以使用任何一个实现相同的功能。

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?

如你所见,他们都做同样的事情。有人能告诉我一个例子,输出参数与返回变量的选择会有所不同吗?

7 个解决方案

#1


13  

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.

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

#2


11  

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参数。

#3


8  

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,返回值通常被忽略。

#4


7  

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返回批处理脚本中的值一样。返回不是真正用于参数传递,而是作为退出过程或查询的方法。根据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。

#5


1  

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.

第三种情况:功能与程序选择适合您需求的程序。

Hope this helps

希望这可以帮助

#6


1  

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.
  • 返回List项的值为0表示成功,任何非零值表示失败。

#7


0  

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进行查询我没有受到影响。

#1


13  

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.

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

#2


11  

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参数。

#3


8  

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,返回值通常被忽略。

#4


7  

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返回批处理脚本中的值一样。返回不是真正用于参数传递,而是作为退出过程或查询的方法。根据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。

#5


1  

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.

第三种情况:功能与程序选择适合您需求的程序。

Hope this helps

希望这可以帮助

#6


1  

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.
  • 返回List项的值为0表示成功,任何非零值表示失败。

#7


0  

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进行查询我没有受到影响。