Please disable your adblock and script blockers to view this page

Search this blog

Wednesday, 21 March 2018

PL/SQL Conditions, IF-ELSE Conditional Statement


Like other programming languages, PL/SQL supports decision making statements, These statements are also called conditional statement

Basic Syntax of IF-ELSE is like this in PL/SQL

IF (Condition 1)

THEN

Statement to execute (if condition 1 is true)

ELSIF (Condition 2)

THEN 

Statement to execute (if condition 2 is true)

ELSE

Statement to execute (if condition 1& 2 both are false)

END IF;

For a better understanding of concept look at these examples


PL/SQL IF-THEN-ELSE Condition-


DECLARE 
--Variables
a integer :=20;
b integer :=30;
BEGIN 
--Condition Check
IF(a < b) 
THEN
-- If condition is true then print this
dbms_output.put_line('B is greater than A');
ELSE
-- If condition is false then print this
dbms_output.put_line('A is greater than B');
END IF;
END;

Output is -


PL/SQL IF-THEN-ELSIF Condition-


DECLARE 
--Variables
a integer :=20;
b integer :=10;
c integer :=25;
BEGIN 
--Condition 1 Check
IF(a < b and a < c) 
THEN
-- If condition 1 is true then print this
dbms_output.put_line('A is smallest');
--Condition 2 Check
ELSIF (b < a and b < c)
THEN
-- If condition 2 is true then print this
dbms_output.put_line('B is smalllest');
END IF;
END;

Output is-


PL/SQL IF-THEN-ELSIF-THEN-ELSE Condition-


DECLARE
--Variables
a integer :=20;
b integer :=10;
c integer :=5;
BEGIN
--Condition 1 Check
IF(a < b and a < c) 
THEN
-- If condition 1 is true then print this
dbms_output.put_line('A is smallest');
--Condition 2 Check
ELSIF (b < a and b < c)
THEN
-- If condition 2 is true then print this
dbms_output.put_line('B is smalllest');
ELSE
-- If condition 1 and 2 both are false then print this
dbms_output.put_line('C is smallest');
END IF;
END;

Output is-


Cheers :) Happy Learning

No comments :

Post a Comment