威凡网全力打造:网页编程、软件开发编程、平面设计、服务器端开发、操作系统等在线学习平台!学编程,上威凡网!
PHP教程>> PHP基础 PHP技巧 PHP实例 PHP文摘 PHP模板 PHP总结
当前位置:首页 > PHP教程 > PHP总结
上一节 下一节
 mysqlprocedure存储过程循环,条件判断范例

mysql procedure存储过程循环,条件判断实例 本文转载自:???? http://www.111cn.net/database/mysql/38878.htm ? ? mysql教程 procedure存储过程循环,条件判断实例 ? mysql delimiter $$ mysql create procedure myproc() ??? - deterministic ??? - begin ?

mysql procedure存储过程循环,条件判断实例

本文转载自:???? http://www.111cn.net/database/mysql/38878.htm

?

?

mysql教程 procedure存储过程循环,条件判断实例

?

mysql> delimiter $$
mysql> create procedure myproc()
??? -> deterministic
??? -> begin
??? ->?? declare counter int default 0;
??? ->
??? ->?? simple_loop: loop
??? ->???? set counter=counter+1;
??? ->???? select counter;
??? ->???? if counter=10 then
??? ->??????? leave simple_loop;
??? ->???? end if;
??? ->?? end loop simple_loop;
??? ->?? select 'i can count to 10';
??? -> end$$
query ok, 0 rows affected (0.00 sec)

mysql>
mysql> delimiter ;
mysql>
mysql> call myproc();
+---------+
| counter |
+---------+
|?????? 1 |
+---------+
1 row in set (0.00 sec)

+---------+
| counter |
+---------+
|?????? 2 |
+---------+
1 row in set (0.02 sec)

+---------+
| counter |
+---------+
|?????? 3 |
+---------+
1 row in set (0.02 sec)

+---------+
| counter |
+---------+
|?????? 4 |
+---------+
1 row in set (0.02 sec)

+---------+
| counter |
+---------+
|?????? 5 |
+---------+
1 row in set (0.02 sec)

+---------+
| counter |
+---------+
|?????? 6 |
+---------+
1 row in set (0.02 sec)

+---------+
| counter |
+---------+
|?????? 7 |
+---------+
1 row in set (0.02 sec)

+---------+
| counter |
+---------+
|?????? 8 |
+---------+
1 row in set (0.02 sec)

+---------+
| counter |
+---------+
|?????? 9 |
+---------+
1 row in set (0.33 sec)

+---------+
| counter |
+---------+
|????? 10 |
+---------+
1 row in set (0.33 sec)

+-------------------+
| i can count to 10 |
+-------------------+
| i can count to 10 |
+-------------------+
1 row in set (0.33 sec)

query ok, 0 rows affected (0.33 sec)


实例二

mysql> create procedure myproc()
??? -> begin
??? ->???? declare i int;
??? ->???? set i=1;
??? ->???? myloop: loop
??? ->????????? set i=i+1;
??? ->????????? if i=10 then
??? ->?????????????????? leave myloop;
??? ->????????? end if;
??? ->???? end loop myloop;
??? ->???? select 'i can count to 10';
??? ->
??? -> end$$
query ok, 0 rows affected (0.00 sec)

mysql>
mysql> delimiter ;
mysql>
mysql> call myproc();
+-------------------+
| i can count to 10 |
+-------------------+
| i can count to 10 |
+-------------------+
1 row in set (0.00 sec)

query ok, 0 rows affected (0.00 sec

带有条件

mysql> create procedure increment (in in_count int)
??? -> begin
??? -> declare count int default 0;
??? ->
??? ->???? increment: loop
??? ->???????? set count = count + 1;
??? ->???????? if count < 20 then
??? ->???????????? iterate increment;
??? ->???????? end if;
??? ->???????? if count > in_count then
??? ->???????????? leave increment;
??? ->???????? end if;
??? ->???? end loop increment;
??? ->???? select count;
??? -> end
??? -> //
query ok, 0 rows affected (0.00 sec)

mysql>
mysql> delimiter ;
mysql> call increment(3);
+-------+
| count |
+-------+
|??? 20 |
+-------+
1 row in set (0.00 sec)

query ok, 0 rows affected (0.00 sec)

.syntaxhighlighter{padding-top:20px;padding-bottom:20px;}

申明:本文章由威凡网编辑整理并发布,如文中有侵权行为,请与本站客服联系(QQ:254677821)!
上一节 下一节
相关教程  
其他教程  
PHP基础
PHP技巧
PHP实例
PHP文摘
PHP模板
PHP总结

Copyright©威凡网 版权所有 苏ICP备2023020142号
站长QQ:254677821