Sub topic 14
CASE in PL/SQL: Statement Versus Expression
This chapter assumes you can write simple and searched CASE expressions in SQL, you know what NULL means, and you can read a basic anonymous PL/SQL block (DECLARE, BEGIN, END). PL/SQL is Oracle's procedural extension to SQL. It has two different CASE constructs that look almost the same. Knowing which one you are writing prevents several syntax errors and one runtime error.
Two constructs with the same keyword
PL/SQL supports both of these:
- A CASE expression produces a single value. You use it wherever a value is allowed, such as the right side of an assignment, an argument to a function, or inside a SQL statement. It ends with
END. - A CASE statement runs one or more statements and produces no value. It is a control-flow construct, like
IF. It ends withEND CASE;.
The two forms are easy to confuse because both have simple and searched variants. The table summarizes the differences.
| Feature | CASE expression | CASE statement |
|---|---|---|
| Purpose | Returns a value | Executes statements |
| Branch contents | A single expression | One or more statements ending in `;` |
| Terminator | `END` | `END CASE;` |
| No match and no ELSE | Returns NULL | Raises `CASE_NOT_FOUND` (ORA-06592) |
| Where it can appear | Anywhere a value is allowed | Only as a standalone statement |
The last two rows of the table are the ones that cause the most trouble in practice.
The simple CASE statement
A simple CASE statement compares one selector expression against a list of values. The selector is evaluated once. The first WHEN whose value equals the selector runs, and the others are skipped.
DECLARE
v_grade VARCHAR2(1) := 'B';
v_text VARCHAR2(20);
BEGIN
CASE v_grade
WHEN 'A' THEN v_text := 'Excellent';
WHEN 'B' THEN v_text := 'Good';
ELSE v_text := 'Unknown';
END CASE;
DBMS_OUTPUT.PUT_LINE(v_text);
END;
/This prints Good. Each branch is a full PL/SQL statement, so it ends with a semicolon. Compare that with the SQL CASE expression, where each branch is a bare value with no semicolon.
A branch may hold several statements:
DECLARE
v_grade VARCHAR2(1) := 'A';
v_text VARCHAR2(20);
v_bonus NUMBER := 0;
BEGIN
CASE v_grade
WHEN 'A' THEN
v_text := 'Excellent';
v_bonus := 500;
WHEN 'B' THEN
v_text := 'Good';
v_bonus := 200;
ELSE
v_text := 'Unknown';
END CASE;
DBMS_OUTPUT.PUT_LINE(v_text || ' / ' || v_bonus);
END;
/This is the main reason to prefer a CASE statement over an expression: one condition drives several actions.
The searched CASE statement
A searched CASE statement has no selector. Each WHEN holds a Boolean condition, and the first one that is true runs.
DECLARE
v_salary NUMBER := 7500;
v_band VARCHAR2(10);
BEGIN
CASE
WHEN v_salary < 5000 THEN v_band := 'Low';
WHEN v_salary < 10000 THEN v_band := 'Medium';
ELSE v_band := 'High';
END CASE;
DBMS_OUTPUT.PUT_LINE(v_band);
END;
/This prints Medium. As in SQL, order matters. A salary of 7500 also satisfies nothing in the ELSE, but it would satisfy a later, broader condition. Only the first true condition runs.
The CASE expression in PL/SQL
You can use a CASE expression on the right side of an assignment. The same example can be written without a statement per branch:
DECLARE
v_salary NUMBER := 7500;
v_band VARCHAR2(10);
BEGIN
v_band := CASE
WHEN v_salary < 5000 THEN 'Low'
WHEN v_salary < 10000 THEN 'Medium'
ELSE 'High'
END;
DBMS_OUTPUT.PUT_LINE(v_band);
END;
/Here the branches are plain values, there is no CASE after END, and the only semicolon comes after the whole assignment. If you add a semicolon inside a branch of an expression, or forget END CASE on a statement, the compiler reports a syntax error such as PLS-00103.
You can also use a CASE expression directly inside a call:
BEGIN
DBMS_OUTPUT.PUT_LINE(
CASE WHEN SYSDATE - TRUNC(SYSDATE) < 0.5
THEN 'Morning'
ELSE 'Afternoon'
END);
END;
/A CASE statement cannot be used this way, because it is not a value.
No match: NULL versus an exception
The two constructs behave differently when nothing matches and no ELSE is given.
- A CASE expression returns NULL.
- A CASE statement raises the
CASE_NOT_FOUNDexception.
DECLARE
v_grade VARCHAR2(1) := 'Z';
v_text VARCHAR2(20) := 'unchanged';
BEGIN
v_text := CASE v_grade
WHEN 'A' THEN 'Excellent'
WHEN 'B' THEN 'Good'
END;
DBMS_OUTPUT.PUT_LINE('Expression result: ' || NVL(v_text, 'NULL'));
CASE v_grade
WHEN 'A' THEN v_text := 'Excellent';
WHEN 'B' THEN v_text := 'Good';
END CASE;
DBMS_OUTPUT.PUT_LINE('Not reached');
EXCEPTION
WHEN CASE_NOT_FOUND THEN
DBMS_OUTPUT.PUT_LINE('No branch matched');
END;
/The output is:
Expression result: NULL
No branch matchedThe expression silently assigned NULL to v_text. The statement stopped the block and jumped to the handler, so Not reached was never printed.
This difference gives you a design choice. A missing ELSE in a CASE statement acts as a built-in check that every expected value was handled, and an unexpected value fails loudly. If you want to ignore unknown values, add an ELSE NULL; branch. A CASE expression never fails this way, so an unexpected input quietly becomes NULL. If that would be a bug, add an ELSE that returns a clear marker value, or raise an error from a function.
How the selector and conditions are evaluated
The evaluation rules match SQL:
- Branches are tested top to bottom, and the first match wins. Later branches are not evaluated.
- In a simple CASE, the selector is evaluated once, and each
WHENvalue is compared with=. - Because the comparison is
=,WHEN NULLnever matches. A comparison with NULL yields UNKNOWN, not TRUE. Use a searched CASE withIS NULLinstead.
DECLARE
v_comm NUMBER := NULL;
v_label VARCHAR2(20);
BEGIN
CASE
WHEN v_comm IS NULL THEN v_label := 'No commission';
ELSE v_label := 'Has commission';
END CASE;
DBMS_OUTPUT.PUT_LINE(v_label);
END;
/If you wrote CASE v_comm WHEN NULL THEN ... here, the branch would never run. In a CASE statement without an ELSE, that mistake would raise CASE_NOT_FOUND.
Choosing between CASE, IF, and the expression form
A CASE statement and an IF ... ELSIF ... ELSE statement can express the same logic. These guidelines help you choose:
- Use
IFfor a few conditions that are unrelated to each other. - Use a simple CASE statement when one variable is compared with several constant values. It reads like a lookup table.
- Use a searched CASE statement for a ladder of range conditions where each branch does real work.
- Use a CASE expression when the only goal is to compute a value for an assignment or a call.
One behavioral difference favors CASE statements: the automatic CASE_NOT_FOUND check described above. IF has no equivalent, so a missing ELSE in an IF chain silently does nothing.
CASE in SQL inside PL/SQL, and one restriction
A SQL statement embedded in PL/SQL uses the SQL CASE expression, with no special syntax:
DECLARE
v_total NUMBER;
BEGIN
SELECT SUM(CASE WHEN salary >= 10000 THEN 1 ELSE 0 END)
INTO v_total
FROM employees;
DBMS_OUTPUT.PUT_LINE('High earners: ' || v_total);
END;
/The CASE statement, however, is PL/SQL only. You cannot place a CASE statement inside a SQL query.
A related point is the Boolean type. PL/SQL has a BOOLEAN type, and a PL/SQL CASE expression can return a Boolean value inside PL/SQL code. SQL has no Boolean column type in Oracle versions before 23ai, so such an expression cannot be used in a plain SELECT list in those versions. Return a string or number instead when a SQL query needs the result.
Common mistakes
- Writing
END;instead ofEND CASE;to close a CASE statement. - Writing
END CASE;to close a CASE expression, or putting semicolons after branch values in an expression. - Using
WHEN NULLin a simple CASE. - Leaving out
ELSEin a CASE statement and being surprised byCASE_NOT_FOUNDat runtime. - Leaving out
ELSEin a CASE expression and being surprised by NULL. - Mixing result types in an expression, such as
THEN 1in one branch andTHEN 'none'in another. This causes a type error, as it does in SQL.
Summary of the decision
Exercise
A block contains CASE v_status WHEN 'OPEN' THEN v_msg := 'Open'; WHEN 'CLOSED' THEN v_msg := 'Closed'; END CASE; and v_status is 'PENDING'. What happens? How would the result differ if the same logic were written as v_msg := CASE v_status WHEN 'OPEN' THEN 'Open' WHEN 'CLOSED' THEN 'Closed' END;?
Show answer
The CASE statement finds no matching WHEN and has no ELSE, so it raises CASENOTFOUND (ORA-06592) and the block stops unless an exception handler catches it. The CASE expression returns NULL instead, so v_msg becomes NULL and execution continues with no error.
Exercise
Rewrite this fragment so it correctly handles a NULL value in v_bonus: CASE v_bonus WHEN NULL THEN v_note := 'None'; ELSE v_note := 'Some'; END CASE;
Show answer
A simple CASE compares with =, so WHEN NULL never matches and the ELSE branch always runs. Use a searched CASE: CASE WHEN v_bonus IS NULL THEN v_note := 'None'; ELSE v_note := 'Some'; END CASE;
Exercise
You need to set both a text label and a numeric bonus depending on a grade. Should you use a CASE statement or a CASE expression, and why?
Show answer
Use a CASE statement. Each branch must assign two variables, and an expression can return only one value. With expressions you would need two separate CASE expressions that repeat the same tests.