There are three types of MySQL Stored Procedure parameters (in, out, inout) _ MySQL

Source: Internet
Author: User
There are three types of MySQL Stored Procedure parameters (in, out, inout) bitsCN.com I. MySQL Stored Procedure parameters (in)
MySQL stored procedure "in" parameter: similar to the value passing of function parameters in C language, MySQL stored procedure may modify this parameter internally, but modify the in type parameter, not visible for callers ).

Drop procedure if exists pr_param_in;
Create procedure pr_param_in
(
In id int -- in-type MySQL Stored Procedure parameters
)
Begin
If (id is not null) then
Set id = id + 1;
End if;
Select id as id_inner;
End;
Set @ id = 10;
Call pr_param_in (@ id );
Select @ id as id_out;
Mysql> call pr_param_in (@ id );

+ ---------- +
| Id_inner |
+ ---------- +
| 11 |
+ ---------- +
Mysql> select @ id as id_out;
+ -------- +
| Id_out |
+ -------- +
| 10 |
+ -------- +
We can see that the input value of user variable @ id is 10. after the stored procedure is executed, the internal value of user variable @ id is 11 (id_inner), but the external variable value is still 10 (id_out ).

II. MySQL Stored Procedure parameters (out)
MySQL stored procedure "out" parameter: transfers a value from the stored procedure to the caller. In a stored procedure, the initial value of this parameter is null, regardless of whether the caller sets a value for the stored procedure parameter.

Drop procedure if exists pr_param_out;
Create procedure pr_param_out
(
Out id int
)
Begin
Select id as id_inner_1; -- the initial id value is null.
If (id is not null) then
Set id = id + 1;
Select id as id_inner_2;
Else
Select 1 into id;
End if;
Select id as id_inner_3;
End;
Set @ id = 10;
Call pr_param_out (@ id );
Select @ id as id_out;
Mysql> set @ id = 10;
Mysql>
Mysql> call pr_param_out (@ id );

+ ------------ +
| Id_inner_1 |
+ ------------ +
| NULL |
+ ------------ +
+ ------------ +
| Id_inner_3 |
+ ------------ +
| 1 |
+ ------------ +
Mysql> select @ id as id_out;
+ -------- +
| Id_out |
+ -------- +
| 1 |
+ -------- +
It can be seen that although we set the user-defined variable @ id to 10, after passing @ id to the stored procedure, the initial value of id is always null (id_inner_1) within the stored procedure ). The last id value (id_out = 1) is returned to the caller.

III. MySQL Stored Procedure parameters (inout)
The inout parameter of the MySQL stored procedure is similar to the out parameter, and can be passed to the caller from the stored procedure. The difference is that the caller can also pass the value to the stored procedure through the inout parameter.

Drop procedure if exists pr_param_inout;
Create procedure pr_param_inout
(
Inout id int
)
Begin
Select id as id_inner_1; -- the id value is the value passed in by the caller.
If (id is not null) then
Set id = id + 1;
Select id as id_inner_2;
Else
Select 1 into id;
End if;
Select id as id_inner_3;
End;
Set @ id = 10;
Call pr_param_inout (@ id );
Select @ id as id_out;
Mysql> set @ id = 10;
Mysql>
Mysql> call pr_param_inout (@ id );

+ ------------ +
| Id_inner_1 |
+ ------------ +
| 10 |
+ ------------ +
+ ------------ +
| Id_inner_2 |
+ ------------ +
| 11 |
+ ------------ +
+ ------------ +
| Id_inner_3 |
+ ------------ +
| 11 |
+ ------------ +
Mysql>
Mysql> select @ id as id_out;
+ -------- +
| Id_out |
+ -------- +
| 11 |
+ -------- +
The result shows that after @ id (10) is passed to the stored procedure, the stored procedure finally returns the calculated result value 11 (id_inner_3) to the caller. The inout parameter of the MySQL stored procedure is similar to that of the C-language function.

Through the above example: if you only want to pass data to the MySQL stored procedure, use the "in" type parameter. if you only return values from the MySQL stored procedure, the "out" type parameter is used. if you need to pass data to the MySQL stored procedure, you need to compress the data before sending it back to us. in this case, you need to use the "inout" type parameter. BitsCN.com

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.