Please disable your adblock and script blockers to view this page

Search this blog

Thursday, 22 March 2018

PL/SQL CASE Statement, Decision Making Statement in PL/SQL


Like real life in programming sometimes we need to execute some code on a specific condition, PL/SQL CASE statement allows us to execute a sequence of instructions based on a selector (A variable, function, expression etc)
and if selector value is equal to value or expression in WHEN clause then corresponding THEN clause will execute and process the statements

The basic syntax for CASE statement is as follows

CASE (Selector)

WHEN condition 1 THEN
statements to execute...

WHEN condition 2 THEN
statements to execute...
...

WHEN condition N THEN
statements to execute...

ELSE
statements to execute...
END;

See this example of using CASE statement in PL/SQL


DECLARE 
--Variable declared as selector
priority char(1):='M';
BEGIN
-- selector passed in CASE 
CASE priority
-- Checking conditions for the value of selector
WHEN 'H' THEN 
dbms_output.put_line('High Priority');
WHEN 'M' THEN 
dbms_output.put_line('Medium Priority');
WHEN 'N' THEN 
dbms_output.put_line('Normal Priority');
END CASE;
END;

Output is-


Cheers :) Happy Learning

No comments :

Post a Comment