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

1.loop
語法:
[:] LOOP
    
END LOOP []begin_labelstatement_listend_label
例子:
CREATE PROCEDURE doiterate(p1 INT) BEGIN label1: LOOP SET p1 = p1 + 1; IF p1 < 10 THEN ITERATE label1;//  can appear only within , , and  statements.  means start the loop again.」 END IF; LEAVE label1;//  can be used within  or loop constructs (, , ). END LOOP label1; SET @x = p1; END;
2.repeat
語法ITERATELOOPREPEATWHILEITERATELEAVEBEGIN ... ENDLOOPREPEATWHILE
[:] REPEAT
    
UNTIL 
END REPEAT []

例子:begin_labelstatement_listsearch_conditionend_label
mysql> 

mysql> 
    -> 
    ->   
    ->   
    ->     
    ->   
    -> 
    -> 
Query OK, 0 rows affected (0.00 sec)

mysql> 
Query OK, 0 rows affected (0.00 sec)

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

3.while
語法delimiter //CREATE PROCEDURE dorepeat(p1 INT)BEGINSET @x = 0;REPEATSET @x = @x + 1;UNTIL @x > p1 END REPEAT;END//CALL dorepeat(1000)//SELECT @x//
[:] WHILE  DO
    
END WHILE []begin_labelsearch_conditionstatement_listend_label
例子:
CREATE PROCEDURE dowhile()
BEGIN
  DECLARE v1 INT DEFAULT 5;

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

4.label標記語法
[:] BEGIN
    []
END []

[:] LOOP
    
END LOOP []

[:] REPEAT
    
UNTIL 
END REPEAT []

[:] WHILE  DO
    
END WHILE []begin_labelstatement_listend_labelbegin_labelstatement_listend_labelbegin_labelstatement_listsearch_conditionend_labelbegin_labelsearch_conditionstatement_listend_label
相關文章
相關標籤/搜索