PL/SQL (Procedural Language/SQL) is a procedural extension of Oracle-SQL that offers language constructs similar to those in imperative programming languages. PL/SQL allows users
and designers to develop complex database applications that require the usage of control structures and procedural elements such as procedures, functions, and modules.
The basic construct in PL/SQL is a block. Blocks allow designers to combine logically related
(SQL-) statements into units. In a block, constants and variables can be declared, and variables
can be used to store query results. Statements in a PL/SQL block include SQL statements,
control structures (loops), condition statements (if-then-else), exception handling, and calls of
other PL/SQL blocks.
PL/SQL blocks that specify procedures and functions can be grouped into packages. A package
is similar to a module and has an interface and an implementation part. Oracle offers several
predefined packages, for example, input/output routines, file handling, job scheduling etc. (see
directory $ORACLE HOME/rdbms/admin).
Another important feature of PL/SQL is that it offers a mechanism to process query results
in a tuple-oriented way, that is, one tuple at a time. For this, cursors are used. A cursor
basically is a pointer to a query result and is used to read attribute values of selected tuples
into variables. A cursor typically is used in combination with a loop construct such that each
tuple read by the cursor can be processed individually.
With PL/SQL, you can use SQL statements to manipulate ORACLE data and flow-of-control statements to process the
data. Moreover, you can declare constants and variables, define subprograms (procedures and functions), and trap
runtime errors. Thus, PL/SQL combines the data manipulating power of SQL with the data processing power of
procedural languages.
PL/SQL is a block-structured language. That is, the basic units (procedures, functions, and anonymous blocks) that
make up a PL/SQL program are logical blocks, which can contain any number of nested sub-blocks. Typically, each
logical block corresponds to a problem or subproblem to be solved.
A block (or sub-block) lets you group logically related declarations and statements. That way you can place
declarations close to where they are used. The declarations are local to the block and cease to exist when the block
completes.
[DECLARE
-- declarations]
BEGIN
-- statements
[EXCEPTION
-- handlers]
END;
In summary, the major goals of PL/SQL are to
increase the expressiveness of SQL,
process query results in a tuple-oriented way,
optimize combined SQL statements,
develop modular database application programs,
reuse program code, and
reduce the cost for maintaining and changing applications.