WHILE statement
The next control flow statement is the WHILE statement. This statement allows us to specify a task to be done repeatedly while a certain condition is valid.
The syntax is:
[name of while statement : ] WHILE condition is true DO
-- some tasks
END WHILE;
Example
DELIMITER $$
CREATE FUNCTION while_demo(x INT, y INT) RETURNS VARCHAR(255) DETERMINISTIC
BEGIN
DECLARE z VARCHAR(255);
SET z = '';
while_example: WHILE x<y DO
SET x = x + 1;
SET z = concat(z, x);
END WHILE;
RETURN z;
END $$
DELIMITER ;
Here, we first declare a local variable z and initialize it to an empty string (an empty string is a string with no content).
Next, we declare a WHILE statement. A WHILE statement can be labelled (but labelling it is optional). Here we label it while_example...