国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 數據庫 > MySQL > 正文

詳解MySQL存儲過程參數有三種類型(in、out、inout)

2024-07-24 12:49:13
字體:
來源:轉載
供稿:網友
一、MySQL 存儲過程參數(in)
MySQL 存儲過程 “in” 參數:跟 C 語言的函數參數的值傳遞類似, MySQL 存儲過程內部可能會修改此參數,但對 in 類型參數的修改,對調用者(caller)來說是不可見的(not visible)。
復制代碼 代碼如下:

drop procedure if exists pr_param_in;
create procedure pr_param_in
(
in id int -- in 類型的 MySQL 存儲過程參數
)
begin
if (id is not null) then
set id = id + 1;
end if;
select id as id_inner;
end;
set @id = 10;
call pr_param_in(@id);
select @id as id_out;
mysql> call pr_param_in(@id);

+----------+
| id_inner |
+----------+
| 11 |
+----------+
mysql> select @id as id_out;
+--------+
| id_out |
+--------+
| 10 |
+--------+
可以看到:用戶變量 @id 傳入值為 10,執(zhí)行存儲過程后,在過程內部值為:11(id_inner),但外部變量值依舊為:10(id_out)。

二、MySQL 存儲過程參數(out)
MySQL 存儲過程 “out” 參數:從存儲過程內部傳值給調用者。在存儲過程內部,該參數初始值為 null,無論調用者是否給存儲過程參數設置值。
復制代碼 代碼如下:

drop procedure if exists pr_param_out;
create procedure pr_param_out
(
out id int
)
begin
select id as id_inner_1; -- id 初始值為 null
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;
set @id = 10;
call pr_param_out(@id);
select @id as id_out;
mysql> set @id = 10;
mysql>
mysql> call pr_param_out(@id);

+------------+
| id_inner_1 |
+------------+
| NULL |
+------------+
+------------+
| id_inner_3 |
+------------+
| 1 |
+------------+
mysql> select @id as id_out;
+--------+
| id_out |
+--------+
| 1 |
+--------+
可以看出,雖然我們設置了用戶定義變量 @id 為 10,傳遞 @id 給存儲過程后,在存儲過程內部,id 的初始值總是 null(id_inner_1)。最后 id 值(id_out = 1)傳回給調用者。

三、MySQL 存儲過程參數(inout)
MySQL 存儲過程 inout 參數跟 out 類似,都可以從存儲過程內部傳值給調用者。不同的是:調用者還可以通過 inout 參數傳遞值給存儲過程。
復制代碼 代碼如下:

drop procedure if exists pr_param_inout;
create procedure pr_param_inout
(
inout id int
)
begin
select id as id_inner_1; -- id 值為調用者傳進來的值
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;
set @id = 10;
call pr_param_inout(@id);
select @id as id_out;
mysql> set @id = 10;
mysql>
mysql> call pr_param_inout(@id);
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 进贤县| 隆德县| 南华县| 鄂尔多斯市| 西乡县| 邵武市| 宁德市| 桓台县| 潼关县| 潍坊市| 金堂县| 平山县| 罗山县| 南平市| 苍梧县| 莒南县| 龙州县| 洛阳市| 大兴区| 农安县| 万荣县| 元朗区| 砀山县| 丹棱县| 连江县| 奉新县| 崇州市| 益阳市| 文昌市| 涞水县| 叶城县| 温州市| 马山县| 青川县| 桂阳县| 云安县| 曲周县| 长宁县| 托克逊县| 象州县| 新巴尔虎右旗|