1. 程式人生 > >MySQL儲存過程中的IN,OUT,INOUT型別 用法

MySQL儲存過程中的IN,OUT,INOUT型別 用法

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)

<pre name="code" class="sql">==================================================================================
## 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型別引數。