PL/SQL NULL Statement πΆΒΆ
Prerequisites: PL/SQL Basics
Mentor's Note: In English, a "Placeholder" is a word used when you don't have the real word yet. In PL/SQL,
NULL;is a command that tells Oracle: "Do absolutely nothing." It sounds useless, but it's actually required by the grammar of the language in certain situations! π‘
π The Scenario: The Under Construction Sign π§ΒΆ
Imagine you are building a house.
- You have a room marked "Future Gym".
- Right now, there is nothing in it.
- But the building inspector says every room must have something inside to pass inspection.
- You put a sign that says "UNDER CONSTRUCTION". That sign is the NULL; statement.
π» 1. Usage: Placeholder for LogicΒΆ
PL/SQL requires that every branch of an IF or CASE has at least one statement.
DECLARE
v_job VARCHAR2(20) := 'INTERN';
BEGIN
IF v_job = 'CEO' THEN
Process_Huge_Bonus;
ELSE
-- We don't have a plan for interns yet, but we need a line of code.
NULL; -- π§ Placeholder
END IF;
END;
π» 2. Usage: Following a LabelΒΆ
Labels (for GOTO) cannot be the last thing in a block. They must be followed by a statement.
BEGIN
IF some_condition THEN
GOTO end_of_program;
END IF;
-- ... more code ...
<<end_of_program>>
NULL; -- β
Required so the label has something to point to
END;
π‘οΈ 3. Why use it? (Architect's Note)ΒΆ
- Readability: Using
NULL;shows that you intentionally decided to do nothing. It's better than leaving a gap which might look like a mistake. - Grammar: It satisfies the PL/SQL compiler's requirement for executable statements.