让我们看看如何在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 // 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)