In PL/SQL CASE statement gives us opportunity to execute the sequence database object. An object can be anything such as function, variable or an expression that the CASE statement checks to a Boolean value.
Case Statement works same like IF and else statement, in case statement we can add all the SQL functions, case statement performance is very fast as compare to decode.
CASE Statement Syntax:
CASE [ expression ]
WHEN condition_1 THEN result_1
WHEN condition_2 THEN result_2
…
WHEN condition_n THEN result_n
ELSE result
END
Example of PL/SQL case statement
Let’s take an example to make it clear:
DECLARE
Phonetic char(1) := ‘A’;
BEGIN
CASE Phonetic
when ‘A’ then dbms_output.put_line(‘Alpha’);
when ‘B’ then dbms_output.put_line(‘Bravo’);
when ‘C’ then dbms_output.put_line(‘Charlie’);
when ‘D’ then dbms_output.put_line(‘Delta’);
when ‘F’ then dbms_output.put_line(‘Foxtrot’);
else dbms_output.put_line(‘Failed’);
END CASE;
END;
After execution below is the result:
Alpha
PL/SQL procedure successfully completed.