Conditional Statements in PL/SQL.
PL/SQL provides following conditional statements:
• IF THEN
• IF THEN ELSE
• IF THEN ELSIF
• Nested IF THEN ELSE
• CASE
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
PL/SQL Part 4
1. P L / S Q L T u t o r i a l - B y E r G u r p r e e t S i n g h
Visit: http://gsbprogramming.blogspot.in
2. P L / S Q L T u t o r i a l - B y E r G u r p r e e t S i n g h
Visit: http://gsbprogramming.blogspot.in
Conditional statements in PL/SQL
Basic structure of a conditional statement is:
PL/SQL provides following conditional statements:
IF THEN
IF THEN ELSE
IF THEN ELSIF
Nested IF THEN ELSE
CASE
3. P L / S Q L T u t o r i a l - B y E r G u r p r e e t S i n g h
Visit: http://gsbprogramming.blogspot.in
IF THEN
Syntax:
IF condition THEN
<statements>
END IF;
Condition must be that statement which results a Boolean value i.e. either true or false
For example
BEGIN
IF true THEN
dbms_output.put_line(‘Condition true’);
END IF;
END;
Will always print:
true
IF THEN ELSE
Syntax:
IF condition THEN
<statements>
ELSE
<statements>
END IF;
Condition must be that statement which results a Boolean value i.e. either true or false
For example:
DECLARE
vA number :=10;
vB number :=8;
BEGIN
IF vA>vB THEN
dbms_output.put_line(‘vA is greater’);
ELSE
dbms_output.put_line(‘vB is greater’);
END IF;
END;
Will print:
vA is greater
4. P L / S Q L T u t o r i a l - B y E r G u r p r e e t S i n g h
Visit: http://gsbprogramming.blogspot.in
IF THEN ELSIF
Syntax:
IF condition THEN
<statements>
ELSIF condition THEN
<statements>
ELSE - -Optional
<statements>
END IF;
Condition must be that statement which results a Boolean value i.e. either true or false
For example
DECLARE
vA number :=10;
vB number :=8;
vC number :=15;
BEGIN
IF vA>vB AND vA>vC THEN
dbms_output.put_line(‘vA is greater’);
ELSIF vB>vC THEN
dbms_output.put_line(‘vB is greater’);
ELSE
dbms_output.put_line(‘vC is greater’);
END IF;
END;
Will print:
vC is greater
Nested IF THEN ELSE
Syntax:
IF condition THEN
IF condition THEN
<statements>
ELSE
<statements>
END IF;
ELSE
<statements>
END IF;
Condition must be that statement which results a Boolean value i.e. either true or false
5. P L / S Q L T u t o r i a l - B y E r G u r p r e e t S i n g h
Visit: http://gsbprogramming.blogspot.in
For example
DECLARE
vA number :=10;
vB number :=8;
vC number :=15;
BEGIN
IF vA>vB THEN
IF vA>vC THEN
dbms_output.put_line(‘vA is greater’);
ELSE
dbms_output.put_line(‘vC is greater’);
END IF;
ELSE
IF vB>vC THEN
dbms_output.put_line(‘vB is greater’);
ELSE
dbms_output.put_line(‘vC is greater’);
END IF;
END IF;
END;
Will print:
vC is greater
CASE
Syntax:
CASE selector
WHEN value1 THEN <statement 1>;
WHEN value2 THEN <statement 2>;
WHEN value3 THEN <statement 3>;
…..
ELSE <default statement>
END CASE;
CASE
WHEN selector=value1 THEN <statement 1>;
WHEN selector=value2 THEN <statement 2>;
WHEN selector=value3 THEN <statement 3>;
…..
ELSE <default statement>
END CASE;
Selector is a variable
For example:
6. P L / S Q L T u t o r i a l - B y E r G u r p r e e t S i n g h
Visit: http://gsbprogramming.blogspot.in
DECLARE
vA number :=10;
BEGIN
CASE vA
WHEN 10 THEN dbms_output.put_line(‘Value of vA is 10’);
ELSE dbms_output.put_line(‘Value of vA is not 10’);
END CASE;
END;
DECLARE
vA number :=10;
BEGIN
CASE
WHEN vA=10 THEN dbms_output.put_line(‘Value of vA is 10’);
ELSE dbms_output.put_line(‘Value of vA is not 10’);
END CASE;
END;
Will print:
Value of vA is 10
Note: When there is no matching case then an error will be shown. So always write ELSE statement i.e.
default statement in CASE, although it is not mandatory but is recommended. For example, the code
below will produce an error:
DECLARE
vA number :=100;
BEGIN
CASE vA
WHEN 10 THEN dbms_output.put_line(‘Value of vA is 10’);
END CASE;
END;
ORA-06592: CASE not found while executing CASE statement