Concept Flow - IF-ELSE in procedures
Start Procedure
Evaluate IF condition
Execute IF block
End Procedure
The procedure starts, checks the IF condition, executes the IF block if true, otherwise executes the ELSE block, then ends.
CREATE PROCEDURE CheckAge(IN age INT) BEGIN IF age >= 18 THEN SELECT 'Adult'; ELSE SELECT 'Minor'; END IF; END;
| Step | Input age | Condition (age >= 18) | Branch Taken | Output |
|---|---|---|---|---|
| 1 | 20 | True | IF block | 'Adult' |
| 2 | 17 | False | ELSE block | 'Minor' |
| 3 | 18 | True | IF block | 'Adult' |
| 4 | 0 | False | ELSE block | 'Minor' |
| 5 | Exit | - | - | Procedure ends |
| Variable | Start | After Step 1 | After Step 2 | After Step 3 | After Step 4 | Final |
|---|---|---|---|---|---|---|
| age | undefined | 20 | 17 | 18 | 0 | - |
| condition (age >= 18) | undefined | True | False | True | False | - |
| output | undefined | 'Adult' | 'Minor' | 'Adult' | 'Minor' | - |
IF-ELSE in SQL procedures: - Use IF condition THEN ... ELSE ... END IF; - Condition is evaluated once per call - IF block runs if condition True - ELSE block runs if condition False - Helps control flow inside procedures