PL/SQL Case Statement:
Switch statement is used to execute a block of statement based on the switch expression value. An expression must be of type int, short, byte or char. A case value should be a constant literal value and cannot be duplicated. Expression value is compared with each case value. If a match found corresponding block of statements will be executed. A break statement is used to terminate the execution of statements. If no case value matches with expression value then default block of statements will be executed. If break statement is not used within case, all matching cases will be executed.
Syntax:
CASE [expression]
WHEN condition1 THEN Block of statements1
WHEN condition2 THEN Block of statements2
...
WHEN conditionn THEN Block of statementsn
ELSE Block of statements
END |
CASE [expression]
WHEN condition1 THEN Block of statements1
WHEN condition2 THEN Block of statements2
...
WHEN conditionn THEN Block of statementsn
ELSE Block of statements
END
Example:
DECLARE
nameChar char(1) := 'J';
BEGIN
CASE nameChar
when 'B' then dbms_output.put_line('Bharat');
when 'R' then dbms_output.put_line('Richi');
when 'S' then dbms_output.put_line('Sahdev');
when 'V' then dbms_output.put_line('Vinod');
when 'H' then dbms_output.put_line('Harish');
when 'M' then dbms_output.put_line('Mahesh');
when 'V' then dbms_output.put_line('Vivek');
when 'A' then dbms_output.put_line('Anil');
when 'J' then dbms_output.put_line('Jai');
else dbms_output.put_line('No such name');
END CASE;
END;
/ |
DECLARE
nameChar char(1) := 'J';
BEGIN
CASE nameChar
when 'B' then dbms_output.put_line('Bharat');
when 'R' then dbms_output.put_line('Richi');
when 'S' then dbms_output.put_line('Sahdev');
when 'V' then dbms_output.put_line('Vinod');
when 'H' then dbms_output.put_line('Harish');
when 'M' then dbms_output.put_line('Mahesh');
when 'V' then dbms_output.put_line('Vivek');
when 'A' then dbms_output.put_line('Anil');
when 'J' then dbms_output.put_line('Jai');
else dbms_output.put_line('No such name');
END CASE;
END;
/
Output: