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)!