MySQL存儲過程當中有IN,OUT,INOUT類型
-----------------------------------
## IN IN參數只用來向過程傳遞信息,爲默認值。
## MySQL存儲過程"in"參數:跟C語言的函數參數的值傳遞相似,MySQL存儲過程內部可能會修改此參數,
## 但in類型參數的修改對調用者(caller)來講是不可見的(not visible)
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)
## 能夠看到用戶變量@id傳入值爲10,執行存儲過程後,在過程內部值爲:11(id_inner),
## 但外部變量值依舊爲:10(id_out)
==================================================================================
## OUT OUT參數只用來從過程傳回信息。
## MySQL存儲過程"out"參數:從存儲過程內部傳值給調用者。
## 在存儲過程內部,該參數初始值爲 null,不管調用者是否給存儲過程參數設置值。
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)
## 能夠看出,雖然咱們設置了用戶定義變量@id爲10,傳遞@id給存儲過程後,在存儲過程內部,
## id的初始值老是 null(id_inner_1)。最後id值(id_out=1)傳回給調用者。
===================================================================================
## INOUT INOUT參數能夠向過程傳遞信息,若是值改變,則可再從過程外調用。
## MySQL存儲過程"inout"參數跟out相似,均可以從存儲過程內部傳值給調用者。
## 不一樣的是:調用者還能夠經過inout參數傳遞至給存儲過程。
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)
## 從結果能夠看出:咱們把 @id(10)傳給存儲過程後,存儲過程最後又把計算結果值11(id_inner_3)
## 傳回給調用者。MySQL存儲過程inout參數的行爲跟C語言函數中的引用傳值相似。
=========================================================================================
經過以上例子:
1) 若是僅僅想把數據傳給MySQL存儲過程,那就用in類型參數;
2) 若是僅僅從MySQL存儲過程返回值,那就用out類型參數;
3) 若是須要把數據傳給MySQL存儲過程通過計算再傳回給咱們,那就用inout類型參數。 mysql