Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
5 views

PL SQL Programmig 2

Uploaded by

P S k
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

PL SQL Programmig 2

Uploaded by

P S k
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 32

CONTROL

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

executed, and if the condition is FALSE or


NULL, then the IF statement does nothing.

Syntax for IF-THEN statement is −

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

dbms_output.put_line('a is less than 20 ' );


ELSE
dbms_output.put_line('a is not less than 20 ' );
END IF;
END;
/
OUTPUT
a is not less than 20

PL/SQL procedure successfully completed.


IF-THEN-ELSIF
 The IF-THEN-ELSIF statement allows you to
choose between several alternatives.
 An IF-THEN statement can be followed by an

optional ELSIF...ELSE statement.


 The ELSIF clause lets you add additional

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:

IF( boolean_expression 1)THEN


IF(boolean_expression 2) THEN
sequence-of-statements;
END IF;
ELSE
else-statements;
END IF;
DECLARE
a number(3) := 100;
b number(3) := 200;
BEGIN
IF( a = 100 ) THEN
IF( b = 200 ) THEN
dbms_output.put_line('Value of a is 100 and b is 200'
);
END IF;
ELSE
dbms_output.put_line('Exact value of a is : ' || a );
dbms_output.put_line('Exact value of b is : ' || b );
ENDIF
END;
/
OUTPUT
Value of a is 100 and b is 200
PL/SQL procedure successfully completed
LOOPS
 There may be a situation when you need to
execute a block of code several number of
times.
 A loop statement allows us to execute a

statement or group of statements multiple


times.

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:

WHILE condition LOOP


sequence_of_statements
END LOOP;
DECLARE
a number(2) := 10;
BEGIN
WHILE a < 20 LOOP
dbms_output.put_line('value of a: ' || a);
a := a + 1;
END LOOP;
END;
/
OUTPUT
value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
value of a: 15
value of a: 16
value of a: 17
value of a: 18
value of a: 19
PL/SQL procedure successfully completed.
FOR LOOP

FOR counter IN initial_value .. final_value LOOP


sequence_of_statements;
END LOOP;
DECLARE
a number(2);
BEGIN
FOR a in 10 .. 20 LOOP
dbms_output.put_line('value of a: ' || a);
END LOOP;
END;
/
OUTPUT
value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
value of a: 15
value of a: 16
value of a: 17
value of a: 18
value of a: 19
value of a: 20
PL/SQL procedure successfully completed.
CASE STATEMENT
CASE selector
WHEN 'value1' THEN Statement1;
WHEN 'value2' THEN Statement2;
WHEN 'value3' THEN Statement3;
...
ELSE Statement n;
END CASE;
DECLARE
grade char(1) := ‘C';
BEGIN
CASE grade
when 'A' then dbms_output.put_line('Excellent');
when 'B' then dbms_output.put_line('Very good');
when 'C' then dbms_output.put_line('Well done');
when 'D' then dbms_output.put_line('You passed');

when 'F' then dbms_output.put_line('Better try


again');
else dbms_output.put_line('No such grade');
END CASE;
END;
/
OUTPUT
Well done
PL/SQL procedure successfully completed.
SEARCHED CASE
CASE
WHEN selector = 'value1' THEN S1;
WHEN selector = 'value2' THEN S2;
WHEN selector = 'value3' THEN S3;
...
ELSE Sn;
END CASE;
DECLARE
grade char(1) := 'B';
BEGIN
case
when grade = 'A' then dbms_output.put_line('Excellent');
when grade = 'B' then dbms_output.put_line('Very good');
when grade = 'C' then dbms_output.put_line('Well done');
when grade = 'D' then dbms_output.put_line('You
passed');
when grade = 'F' then dbms_output.put_line('Better try
again');
else dbms_output.put_line('No such grade');
end case;
END;
/
OUTPUT
Very good
PL/SQL procedure successfully completed.

You might also like