Sunteți pe pagina 1din 7

Control Flow in PL/SQL

PL/SQL allows you to branch and


create loops in a fairly familiar way

kamran@niit.edu.pk

Control Flow in PL/SQL


PL/SQL allows you to branch and create loops in a fairly familiar
way. An IF statement looks like:
IF <condition> THEN
<statement_list>
ELSE
<statement_list>
END IF;
The ELSE part is optional. If you want a multiway branch, use:
IF <condition_1> THEN ...
ELSIF <condition_2> THEN ... ... ...
ELSIF <condition_n> THEN ...
ELSE ...
END IF;

Examples
DECLARE
a NUMBER;
b NUMBER;

DECLARE
a NUMBER;
b NUMBER;
BEGIN
SELECT sum(sal), max(sal) INTO a,b FROM emp;
IF a < b THEN
dbms_output.put_line('Max is Higher');
ELSE
dbms_output.put_line('Sum is higher');
END IF;
END;

BEGIN
SELECT sum(sal), max(sal) INTO a,b FROM emp;
IF a < b THEN
dbms_output.put_line('Max is Higher');
ELSIF b < a THEN
dbms_output.put_line('Sum is higher');
ELSE
dbms_output.put_line('Both are equal');
END IF;
END;

PL/SQL Loops
n

Loops are created with the following:

LOOP
< loop_body
loop_body>
> /* A list of statements. */
END LOOP;
At least one of the statements in <loop_body
<loop_body>
>
should be an EXIT statement of the form
EXIT WHEN <condition>;

LOOP Example
n

The loop breaks if <condition> is true. For example, here


is a way to insert each of the pairs (1, 1) through (100,
100) into a table namely T1:

DECLARE
i NUMBER := 1;
BEGIN
LOOP
INSERT INTO T1 VALUES(i,i
VALUES(i,i);
);
i := i+1;
EXIT WHEN i>100;
END LOOP;
END;

DECLARE
i NUMBER := 1;
BEGIN
LOOP
dbms_output.put_line('hi');
i := i+1;
EXIT WHEN i>100;
END LOOP;
END;

Other useful loop-forming statements:


n

A WHILE loop can be formed with


WHILE <condition> LOOP
< loop_body
loop_body>
>
END LOOP;

A simple FOR loop can be formed with:


FOR <var
<var>
> IN <start>..<finish> LOOP
< loop_body
loop_body>
>
END LOOP;

Here, <var
< var>
> can be any variable; it is local to the forfor- loop
and need not be declared. Also, <start> and <finish> are
constants.

Loop-forming statements
Example
DECLARE
i NUMBER := 1;
BEGIN
LOOP
dbms_output.put_line('hi');
i := i+1;

DECLARE
i NUMBER := 1;
BEGIN
WHILE i < 100 LOOP
dbms_output.put_line('hi');
i := i+1;
END LOOP;
END;

EXIT WHEN i>100;


END LOOP;
END;

A Simple FOR loop in PL/SQL


A simple FOR loop can be formed with:
FOR <var> IN <start>..<finish> LOOP
<loop_body>
END LOOP;

DECLARE
Here, <var
<var>
> can be any
variable; it is local to the
for--loop and need not be
for
declared. Also, <start>
and <finish> are
constants.

BEGIN
For i in 1..100 loop
dbms_output.put_line(i);
END LOOP;
END;

PL/SQL Cursors

Cursors
n

Cursors allow embedded SQL statements


n Result is a set (table) in a temporary work area

Cursor name permits iterative manipulation of rows

Two varieties of cursors


n Implicit
n Explicit
More detailed to write, Permit more advanced manipulations

Example
A SIMPLE CURSOR

A SIMPLE CURSOR
Declare
cursor c1 is
select * from emp
emp;;
BEGIN
for emp_rec in c1 loop
dbms_output.put_line(emp_rec.ename);
dbms_output.put_line(emp_rec.ename
);
end loop;
END;

SQL> create or replace procedure sumsalary IS


cursor c1 is
select * from emp
emp;;

Declaration

salsum integer;
BEGIN
salsum := 0;
for emp_rec in c1 loop
salsum := salsum + emp_rec.sal
emp_rec.sal;;
end loop;
dbms_output.put_line('Salary sum: ' || salsum
salsum);
);
END;
Procedure created.

SQL> execute sumsalary;


Salary sum: 29025
PL/SQL procedure successfully completed.

S-ar putea să vă placă și