MySQL存储过程中的3种循环
来源:互联网 发布:曦力 mac 编辑:程序博客网 时间:2024/06/05 20:29
在MySQL存储过程的语句中有三个标准的循环方式:WHILE循环,LOOP循环以及REPEAT循环。还有一种非标准的循环方式:GOTO,不过这种循环方式最好别用,很容易引起程序的混乱,在这里就不错具体介绍了。
这几个循环语句的格式如下:
WHILE……DO……END WHILE
REPEAT……UNTIL END REPEAT
LOOP……END LOOP
GOTO。
mysql> create procedure pro10()
Query OK, 0 rows affected (0.00 sec)
mysql> delete from t1//
Query OK, 0 rows affected (0.00 sec)
mysql> call pro10()//
Query OK, 1 row affected (0.00 sec)
mysql> select * from t1//
+——-+
| filed |
+——-+
|
|
|
|
|
+——-+
5 rows in set (0.00 sec)
mysql> create procedure pro11()
Query OK, 0 rows affected (0.00 sec)
mysql> delete from t1//
Query OK, 5 rows affected (0.00 sec)
mysql> call pro11()//
Query OK, 1 row affected (0.00 sec) #虽然在这里显示只有一行数据受到影响,但是下面选择数据的话,还是插入了5行数据。
mysql> select * from t1//
+——-+
| filed |
+——-+
|
|
|
|
|
+——-+
5 rows in set (0.00 sec)
一行就是执行结果,实际的作用和使用while编写的存储过程一样,都是插入5行数据。
再来看一下第三个循环控制语句LOOP……END LOOP。编写一个存储过程程序如下:
mysql> create procedure pro12()
Query OK, 0 rows affected (0.00 sec)
从上面这个例子可以看出,使用LOOP编写同样的循环控制语句要比使用while和repeat编写的要复杂一些:在循环内部加入了IF……END IF语句,在IF语句中又加入了LEAVE语句,LEAVE语句的意思是离开循环,LEAVE的格式是:LEAVE 循环标号。
mysql> delete from t1//
Query OK, 5 rows affected (0.00 sec)
mysql> call pro12//
Query OK, 1 row affected (0.00 sec) #虽然说只有一行数据受影响,但是实际上是插入了5行数据。
mysql> select * from t1//
+——-+
| filed |
+——-+
|
|
|
|
|
+——-+
5 rows in set (0.00 sec)
mysql> create procedure pro13()
Query OK, 0 rows affected (0.00 sec)
mysql> create procedure pro14()
Query OK, 0 rows affected (0.00 sec)
ITERATE 迭代
mysql> create procedure pro15()
Query OK, 0 rows affected (0.00 sec)
mysql> delete from t1//
Query OK, 5 rows affected (0.00 sec)
mysql> call pro15//
Query OK, 1 row affected (0.00 sec)
mysql> select * from t1//
+——-+
| filed |
+——-+
|
|
|
|
+——-+
4 rows in set (0.00 sec)
和我们上面分析的结果一样,只插入了数值0,1,2,4。
原文链接:http://blog.sina.com.cn/s/blog_4f925fc30100mw2l.html
- MySQL存储过程中的3种循环
- MySQL存储过程中的3种循环
- MySQL存储过程中的3种循环
- MySQL存储过程中的3种循环
- MySQL存储过程中的3种循环
- MySQL存储过程中的3种循环
- MySQL存储过程中的3种循环
- MySQL存储过程中的3种循环
- MySQL存储过程中的3种循环
- MySQL存储过程中的3种循环
- MySQL存储过程中的3种循环
- MySQL存储过程中的3种循环
- MySQL存储过程中的3种循环
- MySQL存储过程中的3种循环
- MySQL存储过程中的3种循环
- MySQL 存储过程中的循环
- MYSQL存储过程3种循环
- MySQL存储过程中的while循环
- CCTableView刷新数据后显示错乱
- [转载] Android开发优化之——使用软引用和弱引用
- lamp 和wamp 的区别
- 11. 分类、扩展、协议、ARC
- 【转载】Android开发优化之——对界面UI的优化(1)
- MySQL存储过程中的3种循环
- Objective-C学习1
- 【Android学习】Android服务之Service(2)--绑定本地Service
- Python yield 使用浅析
- c++11 线程:让你的多线程任务更轻松
- 【Compute Vision】【Color】图像RGB与YUV转换优化
- python pip 安装
- java 服务 cpu 问题跟进
- Mac App 的发布