Oracle PL/SQL: CASE Statement with Examples

What is CASE Statement?

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

Syntax:

CASE (expression)
 WHEN <valuel> THEN action_blockl;
 WHEN <value2> THEN action_block2;
 WHEN <value3> THEN action_block3;
 ELSE action_block_default;
END CASE;

Example 1: Arithmetic Calculation using Case

In this example, we are going to do 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 Output:

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

SEARCHED CASE Statement

The SEARCHED CASE statement is similar to the CASE statement, rather than using the selector to select the alternative, SEARCHED CASE will directly have the expression defined in the WHEN clause.

Syntax:

CASE
WHEN <expression1> THEN action_blockl; 
WHEN <expression2> THEN action_block2; 
WHEN <expression3> THEN action_block3; 
ELSE action_block_default;
END CASE;

Example 1: Arithmetic Calculation using Searched Case

In this example, we are going to do 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 Output:

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

Summary

TYPE DESCRIPTION USAGE

CASE

Similar to IF-THEN-ELSIF statement. A 'SELECTOR' is used to choose the alternatives instead of Boolean expression.

Used to select from several alternatives using 'SELECTOR'

SEARCHED CASE

CASE statement with no actual 'SELECTOR'. Instead, it contains the actual condition (which evaluates to TRUE/FALSE) that will select the alternatives.

Used to choose from more than two alternatives mostly.

 

YOU MIGHT LIKE: