码迷,mamicode.com
首页 > 数据库 > 详细

mysql流控制语句(LOOP, REPEAT, WHILE)

时间:2016-12-26 18:45:01      阅读:398      评论:0      收藏:0      [点我收藏+]

标签:bsp   cti   标记   create   art   and   while   with   label   

1.loop
语法:
[begin_label:] LOOP
    statement_list
END LOOP [end_label]
例子:
CREATE PROCEDURE doiterate(p1 INT) BEGIN label1: LOOP SET p1 = p1 + 1; IF p1 < 10 THEN ITERATE label1;// ITERATE can appear only within LOOPREPEAT, and WHILE statements. ITERATE means start the loop again.” END IF; LEAVE label1;// LEAVE can be used within BEGIN ... END or loop constructs (LOOPREPEATWHILE). END LOOP label1; SET @x = p1; END;
2.repeat
语法
[begin_label:] REPEAT
    statement_list
UNTIL search_condition
END REPEAT [end_label]

例子:
mysql> delimiter //

mysql> CREATE PROCEDURE dorepeat(p1 INT)
    -> BEGIN
    ->   SET @x = 0;
    ->   REPEAT
    ->     SET @x = @x + 1;
    ->   UNTIL @x > p1 END REPEAT;
    -> END
    -> //
Query OK, 0 rows affected (0.00 sec)

mysql> CALL dorepeat(1000)//
Query OK, 0 rows affected (0.00 sec)

mysql> SELECT @x//
+------+
| @x   |
+------+
| 1001 |
+------+
1 row in set (0.00 sec)

3.while
语法
[begin_label:] WHILE search_condition DO
    statement_list
END WHILE [end_label]
例子:
CREATE PROCEDURE dowhile()
BEGIN
  DECLARE v1 INT DEFAULT 5;

  WHILE v1 > 0 DO
    ...
    SET v1 = v1 - 1;
  END WHILE;
END;

4.label标记语法
[begin_label:] BEGIN
    [statement_list]
END [end_label]

[begin_label:] LOOP
    statement_list
END LOOP [end_label]

[begin_label:] REPEAT
    statement_list
UNTIL search_condition
END REPEAT [end_label]

[begin_label:] WHILE search_condition DO
    statement_list
END WHILE [end_label]




 

mysql流控制语句(LOOP, REPEAT, WHILE)

标签:bsp   cti   标记   create   art   and   while   with   label   

原文地址:http://www.cnblogs.com/wyzs/p/6223230.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!