MySQL存储过程的深入讲解(in、out、inout)


当前第2页 返回上一页

IN

mysql> create procedure test1(in in_id int(2))
 -> begin
 -> select * from stu.a_player where id=in_id;
 -> end $$
Query OK, 0 rows affected (0.00 sec)
mysql> delimiter ;

#将4传递给in_id变量,执行事务
mysql> call test1(4); 
+----+---------+-------+
| id | name | score |
+----+---------+-------+
| 4 | zhaoliu | 90 |
+----+---------+-------+
1 row in set (0.00 sec)

#将6传递给in_id变量,执行事务
mysql> call test1(6);
+----+------+-------+
| id | name | score |
+----+------+-------+
| 6 | keke | 75 |
+----+------+-------+
1 row in set (0.00 sec)

OUT

mysql> delimiter $$
mysql> create procedure test2(out aa int) 
 -> begin
 -> select aa;
 -> set aa=2;
 -> select aa;
 -> end $$
Query OK, 0 rows affected (0.00 sec)
mysql> delimiter ;
#将@aa变量传递给test2 事务
mysql> call test2(@aa);
+------+
| aa |
+------+
| NULL |
+------+
#out向调用者输出参数,不接收输入的参数,所以aa为null
1 row in set (0.00 sec)
+------+
| aa |
+------+
| 2 |
+------+
事务将aa变量设置为2(设置的是全局),则可进行输出
1 row in set (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
mysql> select @aa;
+------+
| @aa |
+------+
| 2 |
+------+
1 row in set (0.00 sec)
#事务外查询变量,已经被修改

IN 、OUT、 INOUT 对比

mysql> delimiter //
mysql> create procedure test3(in num1 int,out num2 int,inout num3 int)
 -> begin
 -> select num1,num2,num3;
 -> set num1=10,num2=20,num3=30;
 -> select num1,num2,num3;
 -> end //
Query OK, 0 rows affected (0.00 sec)
mysql> delimiter ;
mysql> call test3(@num1,@num2,@num3);
+------+------+------+
| num1 | num2 | num3 |
+------+------+------+
| 1 | NULL | 3 |
+------+------+------+
1 row in set (0.00 sec)
+------+------+------+
| num1 | num2 | num3 |
+------+------+------+
| 10 | 20 | 30 |
+------+------+------+
1 row in set (0.00 sec)
Query OK, 0 rows affected (0.00 sec)

in和inout参数会将全局变量的值传入存储过程中,而out参数不会将全局变量的值传入存储过程中。在存储过程使用中,参数值in,out,inout都会发生改变。

mysql> select @num1,@num2,@num3;
+-------+-------+-------+
| @num1 | @num2 | @num3 |
+-------+-------+-------+
|  1 | 20 | 30 |
+-------+-------+-------+
1 row in set (0.00 sec)

调用完存储过程后,发现in参数不会对全局变量的值引起变化,而out和inout参数调用完存储过程后,会对全局变量的值产生变化,会将存储过程引用后的值赋值给全局变量。

in参数赋值类型可以是变量还有定值,而out和inout参数赋值类型必须为变量。

总结

到此这篇关于MySQL存储过程(in、out、inout)的文章就介绍到这了,更多相关MySQL存储过程(in、out、inout)内容请搜索

更多相关Mysql内容来自木庄网络博客


标签:Mysql

返回前面的内容

相关阅读 >>

mysql安装配置jdbc和基础学习

开发过程中mysql常见问题的解决方法

mysql存储过程太慢怎么办

删除mysql服务失败怎么办?

mysql添加用户失败怎么办

mysql数据库服务找不到怎么办

mysql精讲之四:tcl事务控制语句

mysql怎样更改安装路径?

vs2010如何连接mysql数据库过程分享

mysql如何来创建表

更多相关阅读请进入《mysql》频道 >>


数据库系统概念 第6版
书籍

数据库系统概念 第6版

机械工业出版社

本书主要讲述了数据模型、基于对象的数据库和XML、数据存储和查询、事务管理、体系结构等方面的内容。



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...