CONTROL STATEMENTSHere are some of the commonly used control statements in PL/SQL: IF Statement : The ‘IF’ statement is used for conditional execution of code. Syntax :
IF condition THEN
-- Code to execute if the condition is true
ELSE
-- Code to execute if the condition is false
END IF;
Example:
declare
a number := 50;
b number := 25;
begin
if (a>b) then
dbms_output.put_line('A is greater');
else
dbms_output.put_line('B is greater');
end if;
end;
/
A is greater
PL/SQL procedure successfully completed.
CASE Statement : The CASE statement is used for selecting one of several alternatives. Syntax :
CASE
WHEN condition1 THEN
-- Code for condition1
WHEN condition2 THEN
-- Code for condition2
-- ...
ELSE
-- Code to execute if none of the conditions are true
END CASE;
Example:
declare
state char(2):='&state';
begin
case state
when 'TN' then
dbms_output.put_line('Welcome to'||'Tamil Nadu');
when 'MP' then
dbms_output.put_line('Welcome to'||'Madhya Pradesh');
when 'KL' then
dbms_output.put_line('Welcome to'||'Kerala');
else
dbms_output.put_line('Contacts customer care');
end case;
end;
/
Enter value for state: TN
old 2: state char(2):='&state';
new 2: state char(2):='TN';
Welcome to Tamil Nadu
PL/SQL procedure successfully completed.
Nested if..elsif..end if cont…
declare
p_center varchar2(20):='¢er';
p_place varchar2(20):='&place';
P_name varchar2(20):='&name';
begin
if p_center='TESDB' then
if p_place='MOGAPAIR' then
dbms_output.put_line('Welcome to TESDB Mogapair! ‘||P_name);
elsif p_place='VELACHERY' then
dbms_output.put_line('Welcome to TESDB Velachery! ‘||P_name);
else
dbms_output.put_line('Welcome to TESDB '||p_name||'!');
end if;
else
dbms_output.put_line('Sorry! '|| p_name||‘ Login Denied');
end if;
end;
/
Enter value for center: TESDB
old 2: p_center varchar2(20):='¢er';
new 2: p_center varchar2(20):='TESDB';
Enter value for place: VELACHERY
old 3: p_place varchar2(20):='&place';
new 3: p_place varchar2(20):='VELACHERY';
Enter value for name: Sakthi
old 4: P_name varchar2(20):='&name';
new 4: P_name varchar2(20):='Sakthi';
Welcome to TESDB Ambatture! Sakthi
« Previous Next Topic » (PL/SQL - Looping Structures) |