The ITERATE statement is used to skip the remaining part of the current loop iteration and proceed to the next iteration.
Syntax
ITERATE label
Parameters
Parameter |
Note |
|---|---|
| label | Loop tag name, specifying the loop to jump to. |
The ITERATE statement can only be used within LOOP, REPEAT, and WHILE statements. When executed, the ITERATE statement skips the remaining portion of the current iteration and proceeds directly to the next iteration.
Examples
Use ITERATE in a LOOP loop 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