Almost stored procedures that you develop require parameters. The
parameters make the stored procedure more flexible and useful. In MySQL,
a parameter has one of three modes
Let’s practice with some examples to get a better understanding.
The
Suppose, you want to get all offices in the USA, you just need to pass a value (USA) to the stored procedure as follows:

To get all offices in France, you pass the France literal string to the

To get the number of shipped orders, we call the

To get the number of orders that are in process, we call the

How it works.
In this tutorial, we have shown you how to define parameters in stored procedures, and introduced you to different parameter modes including
IN, OUT or INOUT.IN– is the default mode. When you define anINparameter in a stored procedure, the calling program has to pass an argument to the stored procedure. In addition, the value of anINparameter is protected. It means that even the value of theINparameter is changed inside the stored procedure, its original value is retained after the stored procedure ends. In other words, the stored procedure only works on the copy of theINparameter.OUT– the value of anOUTparameter can be changed inside the stored procedure and its new value is passed back to the calling program. Notice that the stored procedure cannot access the initial value of theOUTparameter when it starts.INOUT– anINOUTparameter is the combination ofINparameter andOUTparameter. It means that the calling program may pass the argument, and the stored procedure can modify theINOUTparameter and pass the new value back to the calling program.
- The
MODEcould beIN,OUTorINOUT, depending on the purpose of parameter in the stored procedure. - The
param_nameis the name of the parameter. The name of parameter must follow the naming rules of the column name in MySQL. - Followed the parameter name is its data type and size. Like a variable, the data type of the parameter can by any MySQL data type.
;) if the stored procedure has more than one parameter.Let’s practice with some examples to get a better understanding.
MySQL stored procedure parameter examples
IN parameter example
The following example illustrates how to use theIN parameter in the GetOfficeByCountry stored procedure that selects offices located in a specified country.countryName is the IN parameter
of the stored procedure. Inside the stored procedure, we select all
offices that locate in the country specified by the countryName parameter.Suppose, you want to get all offices in the USA, you just need to pass a value (USA) to the stored procedure as follows:
To get all offices in France, you pass the France literal string to the
GetOfficeByCountry stored procedure as follows:OUT parameter example
The following stored procedure returns the number of orders by order status. It has two parameters:-
orderStatus:INparameter that is the order status which you want to count the orders. total:OUTparameter that stores the number of orders for a specific order status.
CountOrderByStatus stored procedure.CountOrderByStatus stored procedure and pass the order status as Shipped, and also pass an argument ( @total) to get the return value.To get the number of orders that are in process, we call the
CountOrderByStatus stored procedure as follows:INOUT parameter example
The following example demonstrates how to useINOUT parameter in the stored procedure.- The
set_counterstored procedure accepts oneINOUTparameter (count) and oneINparameter (inc). - Inside the stored procedure, we increase the counter (
count) by the value of theincparameter.
set_counter stored procedure:IN, OUT and INOUT.
No comments:
Post a Comment