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)
我的博物馆故事 官方安卓版v1.61.2
我的博物馆故事是一款以消除为主题的经营养成类手游,在这里玩家
专业模拟飞行10 手机版v12.2.4
专业模拟飞行10安卓版是一款飞行休闲手游,顶尖的物理飞行引擎
动物起义战斗模拟器二琳同款 最新版v4.1.1
动物起义战斗模拟器是一个非常有趣的模拟类游戏,玩家可以召唤各
迷你世界七周年 安卓手机版v1.43.0
迷你世界7周年是一款由《迷你世界》官方推出的庆祝特别版本,在
劫后公司无限资源版 v1.0.5.1
劫后公司内置菜单版是游戏的破解版本,在该版本中为玩家提供了内