Inside a stored procedure, you can declare local variables, use conditional logic with IF/ELSEIF/ELSE, and iterate with LOOP, WHILE, or REPEAT. This makes procedures capable of implementing complex server-side logic.
Declaring Variables
Use DECLARE to define a local variable with a data type and optional default value. Variables are scoped to the BEGIN...END block in which they are declared.
Control Flow
- IF ... ELSEIF ... ELSE ... END IF — conditional branching
- CASE ... WHEN ... END CASE — multi-way branching
- WHILE condition DO ... END WHILE — pre-condition loop
- REPEAT ... UNTIL condition END REPEAT — post-condition loop
- LOOP ... END LOOP with LEAVE — infinite loop with explicit exit