程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 數據庫知識 >> MYSQL數據庫 >> MySQL綜合教程 >> MySQL存儲過程LOOP循環語句用法

MySQL存儲過程LOOP循環語句用法

編輯: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>

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved