REPEAT statement
Next, let’s look at the REPEAT statement. A REPEAT statement is also used to perform repetitive tasks.
It repeatedly performs some tasks until the UNTIL condition is met. The syntax of a REPEAT statement is:
[name of repeat statement :] REPEAT
-- do some tasks
UNTIL stop condition is met
END REPEAT;
Example
DELIMITER $$
CREATE FUNCTION repeat_demo(x INT, y INT) RETURNS VARCHAR(255) DETERMINISTIC
BEGIN
DECLARE z VARCHAR(255);
SET z = '';
REPEAT
SET x = x + 1;
SET z = concat(z, x);
UNTIL x>=y
END REPEAT;
RETURN z;
END $$
DELIMITER ;
This REPEAT statement repeats two tasks (SET x = x + 1 and SET z = concat(z, x)) until the x >= y condition is met.
If you run the function with the following statement
SELECT repeat_demo(1, 5);
You’...