PL/SQL Introduction
(Procedural Language /Structured Query
Language)
Prepared by: Asst. Prof. Sweta Jethava
An Introduction to PL/SQL
• PL/SQL is a block structured language that enables developers to combine the
power of SQL with procedural statements.
• All the statements of a block are passed to oracle engine all at once which
increases processing speed and decreases the traffic.
Disadvantages of SQL:
• SQL doesn’t provide the programmers with a technique
of condition checking, looping and branching.
• SQL statements are passed to Oracle engine one at a
time which increases traffic and decreases speed.
• SQL has no facility of error checking during
manipulation of data.
Features of PL/SQL:
1.PL/SQL is basically a procedural language, which provides the
functionality of decision making, iteration and many more features of
procedural programming languages.
2.PL/SQL can execute a number of queries in one block using single
command.
3.One can create a PL/SQL unit such as procedures, functions, packages,
triggers, and types, which are stored in the database for reuse by
applications.
4.PL/SQL provides a feature to handle the exception which occurs in
PL/SQL block known as exception handling block.
5.Applications written in PL/SQL are portable to computer hardware or
operating system where Oracle is operational.
6.PL/SQL Offers extensive error checking.
Differences between SQL and
PL/SQL:
Structure of PL/SQL Block:
PL/SQL identifiers
PL/SQL Control Statements
Conditional Selection Statements
Conditional Selection Statements
Conditional Selection Statements
Basic LOOP Statement
Iterative Control (LOOP)
LOOP statements let you execute a sequence of statements multiple times. You
place the keyword LOOP before the first statement in the sequence and the
keywords END LOOP after the last statement in the sequence. For example:
LOOP
EXIT WHEN i > 100;
END LOOP;
Basic LOOP Statement
Iterative Control (FOR-LOOP)
The FOR-LOOP statement lets you specify a range of integers, then execute a
sequence of statements once for each integer in the range. For example:
FOR i IN 1..order_qty LOOP
UPDATE sales SET custno = customer_id
WHERE serial_num = serial_num_seq.NEXTVAL;
END LOOP;
Basic LOOP Statement
Iterative Control (WHILE-LOOP)
The WHILE-LOOP statement associates a condition with a sequence of
statements. Before each iteration of the loop, the condition is evaluated. If
TRUE, loop continues. If FALSE the loop ends and control passes to the next
statement.
For example:
WHILE salary < 4000 LOOP
SELECT sal, mgr, ename
INTO salary, mgr_num, last_name
FROM emp WHERE empno = mgr_num;
END LOOP;