There are IN, OUT, INOUT types in MySQL stored procedures
-----------------------------------
## IN IN Parameters are only used to pass information to the procedure and are default values.
## MySQL stored procedure "in" parameter: Similar to the value transfer of function parameters in C language, this parameter may be modified inside the MySQL stored procedure,
## But the modification of the in type parameter is invisible to the caller (caller)
mysql>use test;
mysql> drop procedure if exists pr_param_in; Query
OK, 0 rows affected, 1 warning (0.01 sec)
mysql> delimiter //
mysql> create procedure pr_param_in(in id int)
-> begin
- > if (id is not null) then
-> set id=id+1;
-> end if;
-> select id as id_inner;
-> end;
-> //
Query OK, 0 rows affected (0.03 sec)
mysql> delimiter;
mysql> set @id=10;
Query OK, 0 rows affected (0.00 sec)
mysql> call pr_param_in(@id);
+----------+
| id_inner |
+------- ---+
| 11 |
+----------+
1 row in set (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
mysql> select @id as id_out;
+---- ----+
| id_out |
+--------+
| 10 |
+--------+
1 row in set (0.00 sec)
## You can see that the user variable @id is passed in The value is 10. After executing the stored procedure, the value inside the procedure is: 11 (id_inner),
## But the value of the external variable is still: 10 (id_out)
================= ===================================================== ===============
## OUT OUT parameters are only used to return information from the procedure.
## MySQL stored procedure "out" parameters: pass values from inside the stored procedure to the caller.
## Inside the stored procedure, the initial value of this parameter is null, regardless of whether the caller sets a value for the stored procedure parameter.
mysql> drop procedure if exists pr_param_out;
Query OK, 0 rows affected, 1 warning (0.01 sec)
mysql> delimiter //
mysql> create procedure pr_param_out(out id int)
-> begin
-> select id as id_inner_1;
-> 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;
-> //
Query OK, 0 rows affected (0.01 sec)
mysql> delimiter ;
mysql> set @id=10;
Query OK, 0 rows affected (0.00 sec)
mysql> call pr_param_out(@id);
+------------+
| id_inner_1 |
+----- -------+
| NULL |
+------------+
1 row in set (0.01 sec)
+------------+
| id_inner_3 |
+------------+
| 1 |
+------------+
1 row in set (0.01 sec)
Query OK, 0 rows affected (0.01 sec)
mysql > select @id as id_out;
+--------+
| id_out |
+--------+
| 1 |
+--------+
1 row in set (0.00 sec )
## It can be seen that although we set the user-defined variable @id to 10, after passing @id to the stored procedure, inside the stored procedure,
## The initial value of id is always null (id_inner_1). The final id value (id_out=1) is passed back to the caller.
===================================================== ================================
## INOUT INOUT parameters can pass information to the process, if the value changes, it can Then call it from outside the procedure.
## MySQL stored procedure "inout" parameters are similar to out, and can pass values from inside the stored procedure to the caller.
## The difference is: the caller can also pass the inout parameter to the stored procedure.
mysql> drop procedure if exists pr_param_inout;
Query OK, 0 rows affected, 1 warning (0.01 sec)
mysql> delimiter //
mysql> create procedure pr_param_inout(inout id int)
-> begin
-> select id as id_inner_1;
-> 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;
-> //
Query OK, 0 rows affected (0.01 sec)
mysql> delimiter ;
mysql> set @id=10;
Query OK, 0 rows affected (0.00 sec)
mysql> call pr_param_inout(@id);
+------------+
| id_inner_1 |
+------------+
| 10 |
+------------+
1 row in set (0.00 sec)
+------------+
| id_inner_2 |
+------------+
| 11 |
+------------+
1 row in set (0.00 sec)
+------------+
| id_inner_3 |
+------------+
| 11 |
+------------+
1 row in set (0.01 sec)
Query OK, 0 rows affected (0.01 sec)
mysql> select @id as id_out;
+-------+
| id_out |
+--------+
| 11 |
+--------+
1 row in set (0.00 sec)
## As can be seen from the result: we pass @id(10) After giving it to the stored procedure, the stored procedure finally returns the calculation result value 11(id_inner_3)
## to the caller. The behavior of MySQL stored procedure inout parameters is similar to pass-by-reference in C language functions.
===================================================== ========================================================================================================================================================================================================================
_
_ If you pass it to a MySQL stored procedure, then use the in type parameter;
2) If you only return the value from the MySQL stored procedure, then use the out type parameter;
3) If you need to pass the data to the MySQL stored procedure, it will be calculated and then returned to us, Then use the inout type parameter.