MySQL While

MySQL While

MySQL While syntax and example.
The WHILE statement is repeated as long as the condition expression is true.

MySQL While syntax

[ begin_label: ] WHILE condition DO
mysql_code
END WHILE [ end_label ]

While example

CREATE PROCEDURE while_proc(x int)
BEGIN
DECLARE i INT DEFAULT 0;
DECLARE y INT;
SET y = x;
	WHILE y > 0 DO
		IF i=0 THEN 
			SET i = x+1;
		ELSE
			SET i = i+1;
		END IF;	
		SET y = y - 1;
	END WHILE;
	SELECT i;
END;

CALL while_proc(5);

Output

PROCEDURE WHILE_PROC compiled
i
10