Control Flow: Variables, Branches & Loops
Control Flow: Variables, Branches & Loops
SQL is natively a declarative set-based language. However, inside MySQL Stored Programs (procedures, functions, and triggers), you can write full imperative procedural logic using Variables, Conditional Branches, and Iterative Loops.
1. Declaring and Setting Local Variables
Local variables exist only within the BEGIN ... END block where they are declared.
DECLARE statements must appear at the very beginning of the BEGIN ... END block before any operational SQL statements!2. Conditional Branching: IF ... ELSEIF ... ELSE
3. Iterative Loops in Stored Procedures
MySQL supports three primary loop constructs:
A. The WHILE Loop (Pre-Condition Check)
Evaluates the condition before entering the loop body:
B. The REPEAT ... UNTIL Loop (Post-Condition Check)
Executes the loop body at least once, evaluating the condition at the end:
Note: Do not put a semicolon after the UNTIL condition!
C. The LOOP with LEAVE (Break) and ITERATE (Continue)
Multiple Choice Questions
1. Where must DECLARE statements be positioned within a stored procedure BEGIN...END block?
A. Anywhere inside the block B. At the very top before any executable SQL statements C. At the very end before END D. Inside the loop condition Answer: B Explanation: In MySQL stored programs, local variable declarations (DECLARE) must precede any cursor declarations, handlers, or operational statements.
2. How is an IF block properly closed in MySQL stored procedures?
A. FI B. ENDIF C. END IF; D. STOP IF Answer: C Explanation: MySQL requires the explicit syntax END IF; to terminate an IF conditional block.
3. What is the key operational difference between WHILE and REPEAT loops?
A. WHILE evaluates condition at start; REPEAT evaluates condition at end (guaranteeing at least 1 execution) B. REPEAT can only execute 10 times C. WHILE loops cannot be nested D. REPEAT only works with floating point numbers Answer: A Explanation: WHILE checks its predicate prior to execution, while REPEAT executes the body first and tests the UNTIL exit condition afterwards.
4. Which keyword serves as a "break" statement to exit a labeled LOOP construct?
A. BREAK B. EXIT C. LEAVE D. TERMINATE Answer: C Explanation: The LEAVE label_name; statement breaks out of an active loop block.
5. What statement is used to assign the result of a single-row SELECT query into a local variable?
A. FETCH INTO B. SELECT ... INTO C. PULL TO D. EXTRACT INTO Answer: B Explanation: SELECT col1, col2 INTO var1, var2 FROM ... populates local variables directly from query results.
Error Handling & Handlers in Stored Programs
Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
Related Lessons
| Previous Lesson | Next Lesson |
|---|---|
| Procedure Parameters: IN, OUT, and INOUT | Error Handling & Handlers in Stored Programs |
Practice Quiz
Test your understanding of this lesson with 5 questions. Each question has one correct answer.