MySQL存储过程的循环控制指令
来源:互联网 发布:新浪宏观数据 编辑:程序博客网 时间:2024/06/05 06:48
资料出处:http://hi.baidu.com/taoxincheng0/blog/item/ea7b49246d0100278744f9ea.html
在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。
- MySQL存储过程的循环控制指令
- MySql存储过程—逻辑判断,条件控制,循环控制
- MySql存储过程—逻辑判断,条件控制,循环控制
- MySql存储过程—逻辑判断,条件控制,循环控制
- mysql 存储过程循环
- MySql循环插入的存储过程
- MySQL存储过程 游标循环的使用
- mysql存储过程的三种循环
- MySQL存储过程的控制语句
- MySQL存储过程的控制语句
- mysql 函数、存储过、带循环的存储过程
- MySQL 存储过程循环遍历
- mysql存储过程之循环
- mysql存储过程之循环
- MySQL存储过程循环详解
- mysql 存储过程 循环插入
- mysql存储过程中三种循环
- MySQL 存储过程中的循环
- 用C#创建Windows服务(Windows Services)
- 中国二三线城市楼市现“小阳春”
- 美工网站
- C++迭代器(iterator)
- 非常好的objective-c语法讲解
- MySQL存储过程的循环控制指令
- 打开图片链接直接打开QQ对话框
- Windows Live API开发系列课程
- android资料--Android通过手势(多点)缩放和拖拽图片
- Java单例模式
- android消息推送-XMPP
- Mysql 集群
- sql 约束(Constraints)
- windows 位图结构详解