CASE Statement in Oracle PL/SQL with Examples

โšก Smart Summary

PL/SQL CASE Statement selects one alternative from several based on a selector, much like IF-THEN-ELSIF but clearer. A simple CASE matches a selector value, while a searched CASE evaluates a condition in each WHEN clause and runs the first that is true.

  • ๐Ÿ”€ Purpose: CASE chooses one action block from many, using a selector instead of a Boolean chain.
  • ๐ŸŽฏ Selector: The CASE expression is evaluated once and compared against each WHEN value.
  • ๐Ÿ”Ž Searched CASE: Puts a full condition in each WHEN clause and runs the first that is true.
  • ๐Ÿงฎ Any Type: The selector can be a variable, number, or arithmetic expression.
  • ๐Ÿ›Ÿ ELSE: The optional ELSE block runs when no alternative matches.
  • ๐Ÿ”š END CASE: A mandatory END CASE closes the statement.
  • ๐Ÿ—„๏ธ SQL Friendly: Unlike IF, CASE can also be used directly inside SQL statements.

CASE Statement in Oracle PL/SQL

What is a CASE Statement?

A CASE statement is similar to the IF-THEN-ELSIF statement; it selects one alternative based on a condition from the available options.

  • A CASE statement uses a “selector” rather than a Boolean expression to choose the sequence.
  • The value of the expression in the CASE statement is treated as a selector.
  • The expression could be of any type (arithmetic, variables, etc.).
  • Each alternative is assigned a certain pre-defined value (selector), and the alternative whose selector value matches the conditional expression value is executed.
  • Unlike IF-THEN-ELSIF, the CASE statement can also be used in SQL statements.
  • The ELSE block in a CASE statement holds the sequence to execute when none of the alternatives is selected.

Syntax

CASE (expression)
WHEN <value1> THEN action_block1;
WHEN <value2> THEN action_block2;
WHEN <value3> THEN action_block3;
ELSE action_block_default;
END CASE;
  • In the above syntax, the expression returns a value that could be of any type.
  • Each ‘WHEN’ clause is an alternative that has a <value> and an <action_block>.
  • The ‘WHEN’ clause whose value matches the expression is selected, and its <action_block> is executed.
  • The ‘ELSE’ block is optional and holds the <action_block_default> to execute when no alternative matches.
  • ‘END’ marks the end of the CASE statement and is a mandatory part of CASE.

Example 1: Arithmetic Calculation Using CASE

In this example, we do an arithmetic calculation between two numbers, 55 and 5.

DECLARE
a NUMBER := 55;
b NUMBER := 5;
arth_operation VARCHAR2(20) := 'MULTIPLY';
BEGIN
dbms_output.put_line('Program started.');
CASE (arth_operation)
WHEN 'ADD' THEN dbms_output.put_line('Addition of the numbers are: '|| a+b );
WHEN 'SUBTRACT' THEN dbms_output.put_line('Subtraction of the numbers are: '||a-b );
WHEN 'MULTIPLY' THEN dbms_output.put_line('Multiplication of the numbers are: '|| a*b );
WHEN 'DIVIDE' THEN dbms_output.put_line('Division of the numbers are:'|| a/b);
ELSE dbms_output.put_line('No operation action defined. Invalid operation');
END CASE;
dbms_output.put_line('Program completed.');
END;
/

Code Explanation

  • Code line 2: Declaring the variable ‘a’ as ‘NUMBER’ and initializing it with value ’55’.
  • Code line 3: Declaring the variable ‘b’ as ‘NUMBER’ and initializing it with value ‘5’.
  • Code line 4: Declaring the variable ‘arth_operation’ as ‘VARCHAR2’ of size 20 and initializing it with value ‘MULTIPLY’.
  • Code line 6: Printing the statement “Program started”.
  • Code line 7: CASE checks the value of the expression. Here, the value of ‘arth_operation’ is ‘MULTIPLY’, which is treated as the selector.
  • Code line 10: The WHEN clause with value ‘MULTIPLY’ matches the selector, so the controller selects this action block and prints ‘Multiplication of the numbers are: 275’.
  • Code line 13: Marks the end of the CASE statement.
  • Code line 14: Printing the statement “Program completed”.

Code Output

Program started.
Multiplication of the numbers are: 275
Program completed.

SEARCHED CASE Statement

The searched CASE statement is similar to the CASE statement, but rather than using a selector to choose the alternative, the searched CASE has the condition defined directly in the WHEN clause.

  • The first WHEN clause that satisfies the condition is executed, and the controller skips the remaining alternatives.

Syntax

CASE
WHEN <expression1> THEN action_block1;
WHEN <expression2> THEN action_block2;
WHEN <expression3> THEN action_block3;
ELSE action_block_default;
END CASE;
  • In the above syntax, each WHEN clause has a separate <expression> and <action_block>.
  • The WHEN clause for which the expression returns TRUE is executed.
  • The ‘ELSE’ block is optional and holds the <action_block_default> to execute when none of the alternatives is satisfied.
  • ‘END’ marks the end of the CASE statement and is a mandatory part of CASE.

Example 1: Arithmetic Calculation Using Searched CASE

In this example, we do an arithmetic calculation between two numbers, 55 and 5.

DECLARE
a NUMBER := 55;
b NUMBER := 5;
arth_operation VARCHAR2(20) := 'DIVIDE';
BEGIN
dbms_output.put_line('Program started.');
CASE
WHEN arth_operation = 'ADD'
THEN dbms_output.put_line('Addition of the numbers are: '||a+b );
WHEN arth_operation = 'SUBTRACT'
THEN dbms_output.put_line('Subtraction of the numbers are: '|| a-b);
WHEN arth_operation = 'MULTIPLY'
THEN dbms_output.put_line('Multiplication of the numbers are: '|| a*b );
WHEN arth_operation = 'DIVIDE'
THEN dbms_output.put_line('Division of the numbers are: '|| a/b );
ELSE dbms_output.put_line('No operation action defined. Invalid operation');
END CASE;
dbms_output.put_line('Program completed.');
END;
/

Code Explanation

  • Code line 2: Declaring the variable ‘a’ as ‘NUMBER’ and initializing it with value ’55’.
  • Code line 3: Declaring the variable ‘b’ as ‘NUMBER’ and initializing it with value ‘5’.
  • Code line 4: Declaring the variable ‘arth_operation’ as ‘VARCHAR2’ of size 20 and initializing it with value ‘DIVIDE’.
  • Code line 6: Printing the statement “Program started”.
  • Code line 7: The searched CASE statement begins. The clauses for ADD, SUBTRACT, and MULTIPLY are skipped, as their conditions do not match the value of ‘arth_operation’.
  • Code line 14: The WHEN clause expression “arth_operation = ‘DIVIDE'” is satisfied and returns TRUE.
  • Code line 15: The action block of the WHEN clause is executed, and ‘Division of the numbers are: 11’ is printed.
  • Code line 17: Marks the end of the CASE statement.
  • Code line 18: Printing the statement “Program completed”.

Code Output

Program started.
Division of the numbers are: 11
Program completed.

Comparison of CASE and SEARCHED CASE

Both forms choose one branch, but they differ in how the branch is selected, as summarised below.

Type Description Usage
CASE Similar to the IF-THEN-ELSIF statement. A ‘selector’ is used to choose the alternative instead of a Boolean expression. Used to select from several alternatives using a selector.
SEARCHED CASE A CASE statement with no actual selector. Instead, it contains the condition (which evaluates to TRUE or FALSE) that selects the alternative. Used to choose from more than two alternatives, mostly with range conditions.

FAQs

A simple CASE compares one selector value against each WHEN value. A searched CASE has no selector and evaluates a full condition in each WHEN, so it handles ranges and compound tests.

Yes. A CASE expression can appear in a SELECT, WHERE, or ORDER BY clause, which is a key advantage over IF-THEN-ELSIF, a construct that works only inside PL/SQL blocks.

A statement CASE raises the CASE_NOT_FOUND exception if no branch matches and no ELSE is present. Adding an ELSE, even an empty NULL action, avoids this run-time error.

Yes. When the branches compare one expression to fixed values, AI can rewrite the chain as a cleaner CASE. Verify the result, since a CASE without an ELSE behaves differently from a missing ELSIF.

A CASE statement runs an action block per branch and ends with END CASE. A CASE expression returns a single value and ends with END, so it can be assigned to a variable or used in SQL.

Summarize this post with: