Stored Procedure, when to use Output parameter vs Return variable

前端 未结 7 1442
不思量自难忘°
不思量自难忘° 2020-12-28 12:54

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 out

相关标签:
7条回答
  • 2020-12-28 13:20

    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.

    0 讨论(0)
  • 2020-12-28 13:21

    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.

    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

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

    0 讨论(0)
  • 2020-12-28 13:22

    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.

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

    Hope this helps

    0 讨论(0)
  • 2020-12-28 13:24

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

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

    0 讨论(0)
  • 2020-12-28 13:29

    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:

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

    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.

    0 讨论(0)
  • 2020-12-28 13:29

    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 讨论(0)
提交回复
热议问题