The ITERATE statement skips the remaining part of the current iteration and proceeds to the next iteration of the loop.
Syntax
ITERATE label
Parameters
Parameter |
Description |
|---|---|
| label | The label of the loop to which the control is transferred. |
The ITERATE statement can only be used within LOOP, REPEAT, and WHILE statements. It skips the remaining part of the current iteration and proceeds to the next iteration of the loop.
Example
Use the ITERATE statement in a LOOP statement to skip certain iterations.
obclient> DELIMITER //
obclient> CREATE PROCEDURE iterate_example()
BEGIN
DECLARE i INT DEFAULT 0;
DECLARE sum_result INT DEFAULT 0;
loop_label: LOOP
SET i = i + 1;
IF i % 2 = 0 THEN
ITERATE loop_label;
END IF;
SET sum_result = sum_result + i;
IF i >= 10 THEN
LEAVE loop_label;
END IF;
END LOOP loop_label;
SELECT sum_result;
END //
obclient> DELIMITER ;
obclient> CALL iterate_example();
The query result is as follows:
+------------+
| sum_result |
+------------+
| 36 |
+------------+
1 row in set
