Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stored Procedure, when to use Output parameter vs Return variable

Tags:

sql

People also ask

Why do we use output parameter in stored procedure?

The Output Parameters in Stored Procedures are used to return some value or values. A Stored Procedure can have any number of output parameters. The simple logic is this — If you want to return 1 value then use 1 output parameter, for returning 5 values use 5 output parameters, for 10 use 10, and so on.

What is the difference between parameter and output parameter in stored procedure?

An input/output parameter is a parameter that functions as an IN or an OUT parameter or both. The value of the IN/OUT parameter is passed into the stored procedure/function and a new value can be assigned to the parameter and passed out of the module. An IN/OUT parameter must be a variable, not a constant.

Can we use to output parameter in procedure?

However, you can execute a procedure with output parameters and not specify output when executing the procedure. No error is returned, but you can't use the output value in the calling program.

Does stored procedure need return value?

Return Value in SQL Server Stored ProcedureIn default, when we execute a stored procedure in SQL Server, it returns an integer value and this value indicates the execution status of the stored procedure. The 0 value indicates, the procedure is completed successfully and the non-zero values indicate an error.


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.