PL SQL Programmig 2
PL SQL Programmig 2
STRUCTURE -
CONDITIONAL CONTROL,
ITERATIVE CONTROL,
SEQUENTIAL CONTROL
CONDITIONAL STRUCTURE
Decision-making structures require that the
programmer specify one or more conditions
to be evaluated or tested by the program,
along with a statement or statements to be
executed if the condition is determined to be
true, and optionally, other statements to be
executed if the condition is determined to be
false.
I. IF Statement
II. IF-THEN_ELSE
III. IF-THEN-ELSEIF
IV. NESTED IF-THEN-ELSE
IF STATEMENT
The IF statement associates a condition with
a sequence of statements enclosed by the
keywords THEN and END IF.
If the condition is TRUE, the statements get
IF condition THEN
Statements ;
END IF;
DECLARE
a number(2) := 10;
BEGIN
-- check the boolean condition using if statement
IF( a < 20 ) THEN
-- if condition is true then print the following
dbms_output.put_line('a is less than 20 ' );
END IF;
dbms_output.put_line('value of a is : ' || a);
END;
/
OUTPUT
a is less than 20
value of a is : 10
PL/SQL procedure successfully
completed.
IF-THEN_ELSE
A sequence of IF-THEN statements can be
followed by an optional sequence of ELSE
statements, which execute when the condition
is FALSE.
Syntax for the IF-THEN-ELSE statement is
−
IF condition THEN
Staement1;
ELSE
Statement2;
END IF;
DECLARE
a number(3) := 100;
BEGIN
-- check the boolean condition using if
statement
IF( a < 20 ) THEN
-- if condition is true then print the following
conditions.
The syntax of an IF-THEN-ELSIF Statement in PL/SQL
programming language is −
IF(boolean_expression1) THEN
Statement1;
ELSIF( boolean_expression2) THEN
Statement2;
ELSIF( boolean_expression3) THEN
Statement3;
ELSE
Statement4;
END IF;
DECLARE
a number(3) := 100;
BEGIN
IF ( a = 10 ) THEN
dbms_output.put_line('Value of a is 10' );
ELSIF ( a = 20 ) THEN
dbms_output.put_line('Value of a is 20' );
ELSIF ( a = 30 ) THEN
dbms_output.put_line('Value of a is 30' );
ELSE
dbms_output.put_line('None of the values is matching');
END IF;
END;
/
OUTPUT-
None of the values is matching
PL/SQL procedure successfully completed.
NESTED IF-THEN-ELSE:
We can use one IF or ELSE IF statement
inside another IF or ELSE IF statement(s)
Syntax:
I. BASIC LOOP
II. WHILE LOOP
III. FOR LOOP
BASIC LOOP
The syntax of a basic loop in PL/SQL
programming language is −
LOOP
Sequence of statements;
END LOOP;
DECLARE
x number := 10;
BEGIN
LOOP
dbms_output.put_line(x);
x := x + 10;
IF x > 50 THEN
exit;
END IF;
END LOOP;
dbms_output.put_line('After Exit x is: ' || x);
END;
/
OUTPUT
10
20
30
40
50
After Exit x is: 60
PL/SQL procedure successfully completed
WHILE LOOP
Syntax: