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 (@In开发者_Go百科Value 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?
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.
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.
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.
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.
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.
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
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.
精彩评论