Ticker

6/recent/ticker-posts

PL/SQL Control Statements: Conditional Control Statements in PL/SQL

Control structures enable the program to change the logical flow of statements within PUS% with a number of control structures. 

PL/SQL supports four basic programming control structures: 

Control structures

These four structures can be combined in any way to solve any given problem. 

Conditional Control Statements 

One can execute a statement sequence or sequence of statements conditionally with the IF-THEN-ELSE statement. Sometimes it is necessary to take alternative actions, depending on a condition or circumstance.

The conditional controls are:

1) IF-THEN Statements: The simple form of the conditional control statement is the IF-THEN statement.

Syntax:

IF condition THEN

statement(s)

END IF;

The statements that immediately follow the conditional IF statement are executed only if the condition evaluates to True. If the condition evaluates to False, the statements are not processed and are passed over.

2) IF-THEN-ELSE Statements: The syntax of this type of statement is shown below.

Syntax:

IF condition THEN

statement(s)

ELSE

statement(s)

END IF;

The statements that immediately follow the conditional IF statement are executed only if the condition evaluates to True. If the condition evaluates to False, the statements immediately following the ELSE portion are executed. You can have nested IF-THEN-ELSE statements if You want.

3) IF-THEN-ELSEIF Statements: in some situations you need to select an action from several mutually exclusive alternatives. The IF-THEN-ELSEIF statement gives a solution for this. The IF-THEN-ELSEIF statement enables you to have several mutually exclusive conditions.

Syntax:

IF condition1 THEN

statement(s);

ELSEIF condition2 THEN

statement(s);

ELSE

statement(s)

END IF;

This is how this structure works. PL/SQL evaluates the first condition. If it is True, the statements following that are executed and the control passes to the statement after the END IF. If condition) evaluates to False, the ELSEIF clause is tested. If it is True the statements after it are executed and control goes to statement after the END IF. If the condition2 is found False, then control goes to the statements after the ELSE clause