>  기사  >  데이터 베이스  >  MySQL利用LOOP循环语句实现存储的方法教程

MySQL利用LOOP循环语句实现存储的方法教程

WBOY
WBOY원래의
2016-06-07 16:23:091409검색

MySQL存储过程的语句中有三个标准的循环方式:WHILE循环,LOOP循环以及REPEAT循环。还有一种非标准的循环方式:GOTO,不过这种循环方式最好别用,很容易引起程序的混乱,在这里就不错具体介绍了。 今天我们先来看看LOOP循环 mysql mysql delimiter $$ mysql

MySQL存储过程的语句中有三个标准的循环方式:WHILE循环,,LOOP循环以及REPEAT循环。还有一种非标准的循环方式:GOTO,不过这种循环方式最好别用,很容易引起程序的混乱,在这里就不错具体介绍了。

今天我们先来看看LOOP循环

mysql>
mysql> delimiter $$
mysql>
mysql> CREATE PROCEDURE myProc()
    -> BEGIN
    ->
    ->     DECLARE i int;
    ->     SET i=0;
    ->     loop1: LOOP
    ->          SET i=i+1;
    ->          IF i>=10 THEN          /*Last number - exit loop*/
    ->               LEAVE loop1;
    ->          ELSEIF MOD(i,2)=0 THEN /*Even number - try again*/
    ->               ITERATE loop1;
    ->          END IF;
    ->
    ->          SELECT CONCAT(i," is an odd number");
    ->
    ->     END LOOP loop1;
    ->
    ->
    -> END$$
Query OK, 0 rows affected (0.02 sec)

mysql>
mysql> delimiter ;
mysql> call myProc();
+-------------------------------+
| CONCAT(i," is an odd number") |
+-------------------------------+
| 1 is an odd number            |
+-------------------------------+
1 row in set (0.00 sec)

+-------------------------------+
| CONCAT(i," is an odd number") |
+-------------------------------+
| 3 is an odd number            |
+-------------------------------+
1 row in set (0.00 sec)

+-------------------------------+
| CONCAT(i," is an odd number") |
+-------------------------------+
| 5 is an odd number            |
+-------------------------------+
1 row in set (0.00 sec)

+-------------------------------+
| CONCAT(i," is an odd number") |
+-------------------------------+
| 7 is an odd number            |
+-------------------------------+
1 row in set (0.01 sec)

+-------------------------------+
| CONCAT(i," is an odd number") |
+-------------------------------+
| 9 is an odd number            |
+-------------------------------+
1 row in set (0.01 sec)

Query OK, 0 rows affected (0.01 sec)

mysql> drop procedure myProc;
Query OK, 0 rows affected (0.00 sec)

mysql>

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.