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 ).
Copy codeThe Code is as follows:
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.
Copy codeThe Code is as follows:
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.
Copy codeThe Code is as follows:
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.