MYSQL儲存過程-練習4 loop迴圈

_飞翔的荷兰人發表於2024-05-14

建立儲存過程

 1 DELIMITER $$
 2 
 3 CREATE  PROCEDURE `sp_loop`()
 4 BEGIN
 5   DECLARE i INT;
 6   SET i=1;
 7   lp1:LOOP
 8     SELECT i;
 9     SET i=i+1;
10     IF i > 5 THEN
11       LEAVE lp1;
12     END IF;
13        END LOOP;
14 END$$
15 
16 DELIMITER ;

執行儲存過程

 1 mysql> call sp_loop();
 2 +------+
 3 | i    |
 4 +------+
 5 |    1 |
 6 +------+
 7 1 row in set (0.00 sec)
 8 
 9 +------+
10 | i    |
11 +------+
12 |    2 |
13 +------+
14 1 row in set (0.01 sec)
15 
16 +------+
17 | i    |
18 +------+
19 |    3 |
20 +------+
21 1 row in set (0.02 sec)
22 
23 +------+
24 | i    |
25 +------+
26 |    4 |
27 +------+
28 1 row in set (0.02 sec)
29 
30 +------+
31 | i    |
32 +------+
33 |    5 |
34 +------+
35 1 row in set (0.04 sec)
36 
37 Query OK, 0 rows affected (0.04 sec)

相關文章