PL/SQL IF/THEN/ELSE Statement
The IF/THEN statement checks a Boolean value or expression
and if true, executes the statements in the THEN clause.
If the condition is false, the statements in the THEN
clause are skipped and execution jumps to the END IF, and
the expression that is checked must return a true or
false. It can be a simple Boolean variable or a compound
expression joined with AND/OR clauses, and the expression
can even be a PL/SQL function that returns a Boolean
value.
•IF...THEN
•IF...THEN...ELSE
•IF...THEN...ELSIF...ENDIF
•IF…THEN…ELSIF…THEN…ELSE…END IF
•
IF THEN statement
•IF THEN...END IFstatement is a single-level
conditional statement which executes a sequence of
statements based on a given condition.
•Syntax:
IF condition THEN
executable statements
END IF;
•Condition
Expression evaluates to TRUE or FALSE
If TRUE commands execute
Demonstration
5
PL/SQL Comparison Operators
IF THEN ELSE statement
•The
IF-THEN-ELSEstatement is next level of extension
of above the
IF-THENstatement, as it provides the
ability to give direction for FALSE or NULL also.
•Syntax:
IF condition THEN
executable statements 1
ELSE
executable statements 2
END IF;
Demonstration
IF THEN ELSIF ENDIFstatement
• For multiple possible outcomes of a
condition, we
use the
IF THEN ELSIF ENDIFstatement
•Syntax:
IF condition1 THEN
sequence_of_statements1
ELSIF condition2 THEN
sequence_of_statements2
ELSE
sequence_of_statements3
END IF;
Demonstration
Nested IF statement
•When using an ELSIF construct, it is not necessary
to specify what action should be taken if none of
the conditions evaluate to TRUE. In other words, an
ELSE clause is not required in the ELSIF construct.