Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 33

PL/SQL

CONTROL
STRUCTURE
INTRODUCTION
CONTROL STURCTURE CAN BE CLASSIFIED
INTO 3 CATAGARIES

1. CONDITIONAL CONTROL
2. ITERATIVE CONTROL
3. SEQUENTIAL CONTROL
1.CONDITIONAL CONTROL
 PL/SQL allows the use of an IF statement to
control the execution of a block of code.
 This structure tests a condition, depending
on the condition is true or false it decides
the sequence of statements to be executed.
CASE-1 If – Then

SYNTAX :

IF <condition> THEN
Statements
END IF;
Example
Declare
A number := &a;
b number := &b;

Begin
IF (a>b) then
dbms_output.put_line (‘ max no =’ || a);
End if;
End;
/
CASE-2 If – then -else
SYNTAX:
IF <condition> THEN
Statements
ELSE
statements
END IF;
Example
Declare
a number(3):= &a;
b number(3):= &b;
Begin
IF (a>b) then
dbms_output.put_line(‘max no =‘|| a);
Else
dbms_output.put_line(‘max no =‘|| b);
end if;
End;
/
CASE-3 If – then -elsif
SYNTAX:
IF <condition> THEN
Statements
ELSIF <condition> THEN
Statements
ELSE
Statements
END IF;
declare
a number(5):=&a;
b number(5):=&b;
c number(5):=&c;
begin
if (a>b) and (a>c) then
dbms_output.put_line( a ||'Is greater');
elsif (b>a) and (b>c) then
dbms_output.put_line( b ||'Is greater');
elsif (c>a) and (c>b) then
dbms_output.put_line( c ||'Is greater');
end if;
end;
Iterative Control
 These type of control indicates the ability to
repeat or skip section of a code of block.
 A loop marks the sequence of statements
that has to be repeated.
 The keyword loop has to be placed before
the first statement in the sequence of
statements to be repeated, while keyword
end loop is placed after the last statement.
i). Simple Loop

 LOOP statement executes the body


statements multiple times.
 The statements are placed between LOOP –
END LOOP keywords.
SYNTAX
CASE-1

LOOP
Statements
END LOOP;
Example
Begin
loop
dbms_output.put_line(‘RDBMS’);
End loop;
End;
/
ii). EXIT
The EXIT statement forces a loop to
complete unconditionally.
SYNTAX:
exit;
Example
Declare
NUM NUMBER:=1;
Begin
loop
dbms_output.put_line(num);
if (num=5) then
exit;
end if;
Num:=num+1;

End loop;
End;
/
iii). EXIT - WHEN
 The exit – when statement forces a loop to
complete conditionally.

SYNTAX:
Exit when condition;
Example
Declare
NUM NUMBER:=1;
Begin
loop
dbms_output.put_line(num);
Num:=num+1;
Exit when num>5;

End loop;
End;
/
iv). WHILE LOOP
 The while-loop statements executes the
statements in the loop as long as a condition is
true.
 This is similar to LOOP. A condition placed between
WHILE and LOOP is evaluated before each iteration.
 If the condition evaluates to TRUE the statements are
executed and the control resumes at the top of the LOOP.
 If the condition evaluates to FALSE or NULL then
control comes out of the loop.
SYNTAX

WHILE <CONDITION>
LOOP
<statements>
END LOOP;
Example
Declare
NUM NUMBER:=1;
Begin
While num<=5
loop
dbms_output.put_line(num);
num=num+1;
End loop;
End;
/
v). FOR LOOP
 The FOR – LOOP is used to repeatedly execute a
set of statements for certain number of times.
 The variable value starts at the starting value
given and increments by 1(default and can not be
changed) with each iteration.
 The iteration stops when the variable value
reaches end value specified.
SYNTAX

FOR variable IN 1 . . n
LOOP
<loop_body> /* seq^ of stat.*/
END LOOP;
Example
Declare
NUM NUMBER:=1;
Begin
FOR num IN 1..5
loop
dbms_output.put_line(num);
End loop;
End;
/
Example
Declare
NUM NUMBER:=1;
Begin
FOR num IN reverse 1..10
loop
dbms_output.put_line(num);
End loop;
End;
/
Factorial of number:

Declare
Num Number(3):=&num;
i number(3);
Fact number(5):=1;
Begin
FOR i IN 1..num loop
fact:=fact*i;
End loop;
dbms_output.put_line(fact);

End;
3.SEQUENTAIL CONTROL
THE GOTO STATEMENT
 The GOTO statement changes the Flow of control
whithin a pl/sql block.
 This statement allows the execution o a section of
code,which is not in the normal flow o control.
 The entry point into such a block of code is
marked using the <<userdefined name>>.
 The GOTO statement can then make use of this
userdefind name to jump into that block of code.
 The GOTO statement is used for doing
unconditional branching to a named label.
Its frequent usage is not recommended. We
should have at least one executable
statement following the label.
 GOTO statements can some time result in
complex, unstructured code making it
difficult to understand.
SYNTAX
GOTO<<CODEBLOCK NAME>>
EXAMPLE
Declare
NUM NUMBER:=1;
Begin
IF Num:=1 THEN
Goto hello;
End if;
<<hello>>
dbms_output.put_line(‘this is goto block’);
End;

/
EXAMPLE
Declare
a NUMBER :=&a;

Begin
IF a>0 THEN
Goto pos;
Else
Goto Neg;
End if;
<<pos>>
dbms_output.put_line(‘positive number’);
Return;
<<Neg>>
dbms_output.put_line(‘Negative number’);
End;

/
WAP to print name of student id 123.
 DECLARE
 v_first_name VARCHAR2(35);
 v_last_name VARCHAR2(35);
 BEGIN
 SELECT first_name, last_name
 INTO v_first_name, v_last_name
 FROM student
 WHERE student_id = 123;
 DBMS_OUTPUT.PUT_LINE
 ('Student name: '||v_first_name||' '||v_last_name);
 EXCEPTION
 WHEN NO_DATA_FOUND THEN
 DBMS_OUTPUT.PUT_LINE
 ('There is no student with student id 123');
 END;
THE END
Example
Declare
Day varchar2(5):=to_char(sysdate,’dy’);
Begin
IF day=‘tues’ then
dbms_output.put_line(‘Enjoy your Tuesday’);
Elsif day=‘sun’ then
dbms_output.put_line (‘Enjoy your sunday’);
Else
dbms_output.put_line (‘Enjoy your day’);
End if;
End;
/

You might also like