Sub topic 14
Choosing the Right Control-Flow Construct
Assume you know basic SQL and the structure of an anonymous PL/SQL block. You should also be comfortable declaring variables, assigning values, and running a block with DBMS_OUTPUT enabled.
PL/SQL gives you several ways to control execution. Choose based on what controls the decision:
- Use
IFwhen conditions are Boolean expressions. - Use
CASEwhen several alternatives should be presented clearly. - Use a basic
LOOPwhen the stopping point is determined inside the loop. - Use a
WHILE LOOPwhen the loop should continue while a condition is true. - Use a numeric
FOR LOOPwhen processing a known integer range. - Use a cursor
FOR LOOPwhen processing rows returned by a query.
A decision guide
| Situation | Clearest construct | Main reason |
|---|---|---|
| One condition must be true | `IF` | Direct Boolean decision |
| Several conditions are tested in order | `IF ELSIF` or searched `CASE` | Expresses ordered rules |
| One value is compared with fixed alternatives | Simple `CASE` | Groups the alternatives |
| A counter has a known range | Numeric `FOR LOOP` | Manages the counter automatically |
| Rows must be processed one at a time | Cursor `FOR LOOP` | Opens, fetches, and closes automatically |
| The stopping condition is checked before each pass | `WHILE LOOP` | May execute zero times |
| The stopping condition is discovered during a pass | Basic `LOOP` with `EXIT` | Executes at least once |
A searched CASE and an IF ELSIF chain can often express the same logic. Prefer the one that makes the rule structure easiest to read. Use CASE when the branches describe mutually exclusive categories. Use IF when conditions involve different actions or complex Boolean logic.
Prefer clarity over cleverness
Keep each branch focused. Deeply nested IF statements are harder to test, so combine related conditions carefully or move a rule into a named procedure. Remember that comparisons involving NULL do not produce TRUE; use IS NULL or IS NOT NULL when missing values matter.
For loops, make the termination rule obvious. A WHILE LOOP can run zero times, while a basic LOOP runs once before it can exit. A numeric FOR LOOP owns its counter, so do not try to assign to the counter inside the loop. A cursor FOR LOOP is concise for row-by-row processing, but it does not automatically make row-by-row processing efficient.
When a loop should be SQL
A loop around INSERT, UPDATE, or DELETE often performs one SQL statement per row. A single set-based SQL statement can usually process all qualifying rows in one operation. This reduces context switches between the PL/SQL engine and SQL engine.
Use a loop when each row needs procedural decisions, calls to procedures, or state that cannot be expressed clearly in SQL. Otherwise, first ask whether WHERE, CASE, joins, or analytic functions can express the operation as one statement.
BEGIN
UPDATE employees
SET salary = salary * 1.05
WHERE department_id = 50
AND performance_score >= 4;
DBMS_OUTPUT.PUT_LINE(SQL%ROWCOUNT || ' employees updated');
END;
/Exercise
You must process every employee returned by a query, and each employee may require a different procedural action. Which construct is the natural starting point, and what should you check before using it?
Show answer
Start with a cursor FOR LOOP. Check whether the work can instead be expressed as one set-based SQL statement. Use the loop when per-row procedural work is genuinely required.