首页 > 数据库 > MySQL > MySQL 存储过程参数IN OUT INOUT对比
mysql> set @num=1;
Query OK, 0 rows affected (0.00 sec)
mysql> call p_in(@num);
+------+
| num |
+------+
| 1 |
+------+
1 row in set (0.00 sec)
+------+
| num |
+------+
| 100 |
+------+
1 row in set (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
此时再次查询@num发现@num仍为1:
mysql> select @num;
+------+
| @num |
+------+
| 1 |
+------+
1 row in set (0.00 sec)
所以 IN 参数只是将变量在存储过程内部做了修改,并没有影响到外部,@num仍为1。
mysql> set @num=1;
Query OK, 0 rows affected (0.00 sec)
mysql> call p_out(@num);
+------+
| num |
+------+
| NULL |
+------+
1 row in set (0.00 sec)
+------+
| num |
+------+
| 100 |
+------+
1 row in set (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
此时发现,过程的第一个select num为NULL,意味着@num无论是否有传入的外部值,其初始值在过程内都为NULL。mysql> select @num;
+------+
| @num |
+------+
| 100 |
+------+
1 row in set (0.00 sec)
mysql> set @num=1;
Query OK, 0 rows affected (0.00 sec)
mysql> call p_inout(@num);
+------+
| num |
+------+
| 1 |
+------+
1 row in set (0.00 sec)
+------+
| num |
+------+
| 100 |
+------+
1 row in set (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
发现返回结果与p_in一致,说明@num为1的值,已经传入存储过程。
再次查询调用过程后的@num,发现@num为100,此时又与p_out一致,同样,说明OUT参数将参数值传回给调用者。
mysql> select @num;
+------+
| @num |
+------+
| 100 |
+------+
1 row in set (0.00 sec)
mysql> drop procedure p_in ;
Query OK, 0 rows affected (0.00 sec)
mysql> drop procedure p_inout ;
Query OK, 0 rows affected (0.00 sec)
mysql> drop procedure p_out ;
Query OK, 0 rows affected (0.00 sec)

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/29773961/viewspace-1769098/ ,如需转载,请注明出处,否则将追究法律责任。