0

0

MySQL存储过程中的IN,OUT,INOUT类型_MySQL

php中文网

php中文网

发布时间:2016-06-01 13:09:46

|

1152人浏览过

|

来源于php中文网

原创

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) 

SPLASH
SPLASH

将音乐制作的乐趣带给每个人。

下载
## 传回给调用者。MySQL存储过程inout参数的行为跟C语言函数中的引用传值类似。
=========================================================================================
通过以上例子:
1)  如果仅仅想把数据传给MySQL存储过程,那就用in类型参数;
2)  如果仅仅从MySQL存储过程返回值,那就用out类型参数;
3)  如果需要把数据传给MySQL存储过程经过计算再传回给我们,那就用inout类型参数。

本站声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

相关专题

更多
虚拟号码教程汇总
虚拟号码教程汇总

本专题整合了虚拟号码接收验证码相关教程,阅读下面的文章了解更多详细操作。

29

2025.12.25

错误代码dns_probe_possible
错误代码dns_probe_possible

本专题整合了电脑无法打开网页显示错误代码dns_probe_possible解决方法,阅读专题下面的文章了解更多处理方案。

20

2025.12.25

网页undefined啥意思
网页undefined啥意思

本专题整合了undefined相关内容,阅读下面的文章了解更多详细内容。后续继续更新。

37

2025.12.25

word转换成ppt教程大全
word转换成ppt教程大全

本专题整合了word转换成ppt教程,阅读专题下面的文章了解更多详细操作。

6

2025.12.25

msvcp140.dll丢失相关教程
msvcp140.dll丢失相关教程

本专题整合了msvcp140.dll丢失相关解决方法,阅读专题下面的文章了解更多详细操作。

2

2025.12.25

笔记本电脑卡反应很慢处理方法汇总
笔记本电脑卡反应很慢处理方法汇总

本专题整合了笔记本电脑卡反应慢解决方法,阅读专题下面的文章了解更多详细内容。

6

2025.12.25

微信调黑色模式教程
微信调黑色模式教程

本专题整合了微信调黑色模式教程,阅读下面的文章了解更多详细内容。

5

2025.12.25

ps入门教程
ps入门教程

本专题整合了ps相关教程,阅读下面的文章了解更多详细内容。

4

2025.12.25

苹果官网入口直接访问
苹果官网入口直接访问

苹果官网直接访问入口是https://www.apple.com/cn/,该页面具备0.8秒首屏渲染、HTTP/3与Brotli加速、WebP+AVIF双格式图片、免登录浏览全参数等特性。本专题为大家提供相关的文章、下载、课程内容,供大家免费下载体验。

218

2025.12.24

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号