Introduction
Variables let you store values to use later. Control flow helps you decide what to do next based on conditions.
DECLARE variable_name datatype [DEFAULT value]; SET variable_name = value; IF condition THEN -- statements ELSEIF condition THEN -- statements ELSE -- statements END IF; WHILE condition DO -- statements END WHILE;
DECLARE count INT DEFAULT 0; SET count = 5;
IF count > 0 THEN SET message = 'Positive'; ELSE SET message = 'Zero or Negative'; END IF;
WHILE count > 0 DO SET count = count - 1; END WHILE;
DELIMITER $$ CREATE PROCEDURE demo_variables() BEGIN DECLARE counter INT DEFAULT 3; DECLARE result VARCHAR(20); WHILE counter > 0 DO IF counter = 3 THEN SET result = 'Three'; ELSEIF counter = 2 THEN SET result = 'Two'; ELSE SET result = 'One'; END IF; SELECT CONCAT('Count is: ', counter, ', Result is: ', result) AS output; SET counter = counter - 1; END WHILE; END$$ DELIMITER ; CALL demo_variables();
DECLARE counter INT DEFAULT 1;
WHILE counter <= 3 DO
SET counter = counter + 1;
END WHILE;
SELECT counter;
SELECT counter; statement?DECLARE total INT;
SET total = 0
WHILE total < 5 DO
SET total = total + 1;
END WHILE;
Pass the test and this pattern moves unknown