1.loop
Grammar:
[ begin_label :] LOOP END LOOP [ end_label ] statement_list
example:
CREATE PROCEDURE doiterate (P1 INT) BEGIN label1:loop SET p1 = p1 + 1; IF P1 < iterate label1;//iterate can appear only Within loop , repeat , And while statements. iterate means " start the loop again. " END IF; LEAVE label1;//leave can be used Within begin ... END or Loop constructs (loop , repeat , while ). END LOOP Label1; SET @x = p1; END;
2.repeat
Syntax
[ Begin_label :] REPEAT statement_list UNTIL search_condition END REPEAT [ End_ Label ]
Example:
mysql> delimiter// mysql> Strong class= "Userinput" > CREATE PROCEDURE dorepeat (P1 INT) , < Code>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 (+)/// Strong>query OK, 0 rows Affected (0.00 sec) mysql> SELECT @x// +----- -+| @x |+------+| 1001 |+------+1 Row in Set (0.00 sec)
3.while
Syntax
begin_label [ search_condition do ENDwhile end_label [] statement_list
Example:
CREATE PROCEDURE dowhile () BEGIN DECLARE v1 INT DEFAULT 5; While v1 > 0 do ... SET v1 = v1-1; END while; END;
4.label Markup Syntax
[ begin_label :] BEGIN [ statement_list end_label ]end begin_label []: Loop end_label END Loop [] [ statement_list begin_label :] REPEAT statement_list END REPEAT [ begin_label ] [ end_label search_condition search_condition do END while [ end_label ] statement_list
MySQL Stream control statement (loop, REPEAT, while)