MySQL 如何循环存储过程
让我们看看如何在MySQL中循环存储过程。
mysql> DELIMITER //
mysql> CREATE PROCEDURE do_WhileDemo(LastValue INT)
-> BEGIN
-> SET @loop = 0;
-> REPEAT
-> SET @loop= @loop+ 1;
-> select @loop;
-> UNTIL @loop >LastValue
-> END REPEAT;
-> END //
Query OK, 0 rows affected (0.17 sec)
mysql> DELIMITER ;
现在使用CALL命令调用存储过程。
查询如下
mysql> call do_WhileDemo(10);
以下是输出结果。
+-------+
| @loop |
+-------+
| 1 |
+-------+
1 row in set (0.00 sec)
+-------+
| @loop |
+-------+
| 2 |
+-------+
1 row in set (0.01 sec)
+-------+
| @loop |
+-------+
| 3 |
+-------+
1 row in set (0.02 sec)
+-------+
| @loop |
+-------+
| 4 |
+-------+
1 row in set (0.03 sec)
+-------+
| @loop |
+-------+
| 5 |
+-------+
1 row in set (0.04 sec)
+-------+
| @loop |
+-------+
| 6 |
+-------+
1 row in set (0.04 sec)
+-------+
| @loop |
+-------+
| 7 |
+-------+
1 row in set (0.05 sec)
+-------+
| @loop |
+-------+
| 8 |
+-------+
1 row in set (0.06 sec)
+-------+
| @loop |
+-------+
| 9 |
+-------+
1 row in set (0.07 sec)
+-------+
| @loop |
+-------+
| 10 |
+-------+
1 row in set (0.08 sec)
+-------+
| @loop |
+-------+
| 11 |
+-------+
1 row in set (0.09 sec)
Query OK, 0 rows affected (0.10 sec)
阅读更多:MySQL 教程
极客教程