loop in my sql code example
Example 1: mysql loop
-- MySQL LOOP, ITERATE, LEAVE, loop label
CREATE PROCEDURE LoopDemo()
BEGIN
DECLARE x INT;
DECLARE str VARCHAR(255);
SET x = 1;
SET str = '';
loop_label: LOOP
IF x > 10 THEN
LEAVE loop_label;
END IF;
SET x = x + 1;
IF (x mod 2) THEN
ITERATE loop_label;
ELSE
SET str = CONCAT(str,x,',');
END IF;
END LOOP;
SELECT str;
END$$
Example 2: what does iterate do in mysql
-- MySQL
-- Iterate statement is used to skip the current loop iteration and
-- start a new iteration
[label]: LOOP
...
-- terminate the loop
IF condition THEN
ITERATE [label];
END IF;
...
END LOOP;