Sub topic 1
Conditions, Booleans, and NULL
This chapter covers how PL/SQL decides which code to run. It explains how conditions evaluate, how IF and CASE choose between branches, and how NULL changes the result.
Prerequisites
You should be comfortable with:
- Basic SQL: SELECT, WHERE, and simple DML
- The structure of an anonymous PL/SQL block (DECLARE, BEGIN, EXCEPTION, END)
- Declaring and assigning scalar variables
- Running a script in SQL*Plus, SQLcl, or SQL Developer with DBMS_OUTPUT enabled
Conditions, Booleans, and NULL
A condition is an expression that evaluates to TRUE, FALSE, or NULL. PL/SQL has no separate result for "unknown" in the way SQL does, so NULL is the third value that a comparison can produce.
Any comparison with a NULL operand returns NULL. An IF statement runs its branch only when the condition is TRUE, so NULL falls through to ELSE.
DECLARE
l_bonus NUMBER := NULL;
BEGIN
IF l_bonus > 100 THEN
DBMS_OUTPUT.PUT_LINE('Large bonus');
ELSE
DBMS_OUTPUT.PUT_LINE('ELSE branch: the condition was NULL, not TRUE');
END IF;
IF l_bonus IS NULL THEN
DBMS_OUTPUT.PUT_LINE('Test for NULL explicitly with IS NULL');
END IF;
END;
/Always test for a missing value with IS NULL or IS NOT NULL.
Making a Decision with IF-THEN
An IF-THEN statement runs a block of statements only when its condition is TRUE. The statement ends with END IF.
Choosing Between Branches with ELSE and ELSIF
ELSE runs when the condition is FALSE or NULL. ELSIF adds another test. PL/SQL checks the conditions in order and runs only the first branch that is TRUE.
DECLARE
l_score NUMBER := 72;
l_grade VARCHAR2(1);
BEGIN
IF l_score >= 90 THEN
l_grade := 'A';
ELSIF l_score >= 80 THEN
l_grade := 'B';
ELSIF l_score >= 70 THEN
l_grade := 'C';
ELSE
l_grade := 'F';
END IF;
DBMS_OUTPUT.PUT_LINE('Grade: ' || l_grade);
END;
/Order the conditions from most specific to least specific. Otherwise an early broad test hides the later ones.
Nested IF Statements and Short-Circuit Evaluation
You can place an IF inside another IF. PL/SQL also evaluates AND and OR with short-circuit evaluation, so it stops as soon as the result is known. Put a guard condition first to avoid errors such as division by zero.
DECLARE
l_total NUMBER := 100;
l_divisor NUMBER := 0;
BEGIN
IF l_divisor <> 0 AND l_total / l_divisor > 2 THEN
DBMS_OUTPUT.PUT_LINE('Ratio is above 2');
ELSE
DBMS_OUTPUT.PUT_LINE('Guard prevented division by zero');
END IF;
END;
/The Simple CASE Statement
A simple CASE compares one selector value with each WHEN value in turn. It runs the first matching branch. Use ELSE for values that match nothing.
DECLARE
l_status VARCHAR2(1) := 'A';
BEGIN
CASE l_status
WHEN 'A' THEN DBMS_OUTPUT.PUT_LINE('Active');
WHEN 'I' THEN DBMS_OUTPUT.PUT_LINE('Inactive');
ELSE DBMS_OUTPUT.PUT_LINE('Unknown');
END CASE;
END;
/Exercise
The variable lscore holds NULL and you write IF lscore < 50 THEN ... ELSE ... END IF. Which branch runs, and why?
Show answer
The ELSE branch runs. The comparison l_score < 50 returns NULL, not TRUE, and IF runs its THEN branch only for TRUE.