0% found this document useful (0 votes)
48 views

Control Flow in PL/SQL

The document discusses control flow and looping in PL/SQL. It states that PL/SQL allows branching with IF/ELSE statements and looping with LOOP, WHILE, and FOR constructs. Examples are provided to illustrate how to use IF/ELSE statements to compare values and branch accordingly, and how to use LOOP, WHILE, and FOR loops to iterate through a range of numbers or database results. The document also provides an overview of cursors in PL/SQL, stating that cursors allow iterative processing of database result sets, and gives a basic example of using a cursor in a FOR loop to output records.

Uploaded by

cavad565
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views

Control Flow in PL/SQL

The document discusses control flow and looping in PL/SQL. It states that PL/SQL allows branching with IF/ELSE statements and looping with LOOP, WHILE, and FOR constructs. Examples are provided to illustrate how to use IF/ELSE statements to compare values and branch accordingly, and how to use LOOP, WHILE, and FOR loops to iterate through a range of numbers or database results. The document also provides an overview of cursors in PL/SQL, stating that cursors allow iterative processing of database result sets, and gives a basic example of using a cursor in a FOR loop to output records.

Uploaded by

cavad565
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Control Flow in PL/SQL

PL/SQL allows you to branch and


create loops in a fairly familiar way

[email protected]

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.

You might also like