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), mysql
## 但外部變量值依舊爲:10(id_out)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給存儲過程後,在存儲過程內部, sql
## id的初始值老是 null(id_inner_1)。最後id值(id_out=1)傳回給調用者。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類型參數。