This guide explains the concepts behind the PL/SQL language and shows, with examples, how to use various language features.

This Preface contains these topics:


PL/SQL, Oracle's procedural extension of SQL, is an advanced fourth-generation programming language (4GL). It offers software-engineering features such as data encapsulation, overloading, collection types, exceptions, and information hiding. PL/SQL also supports rapid prototyping and development through tight integration with SQL and the Oracle database.

Anyone developing PL/SQL-based applications for Oracle should read this book. This book is intended for programmers, systems analysts, project managers, database administrators, and others who need to automate database operations. People developing applications in other languages can also produce mixed-language applications with parts written in PL/SQL.

To use this guide effectively, you need a working knowledge of the Oracle database, the SQL language, and basic programming constructs such as IF-THEN comparisons, loops, procedures, and functions.

Documentation Accessibility

Our goal is to make Oracle products, services, and supporting documentation accessible, with good usability, to the disabled community. To that end, our documentation includes features that make information available to users of assistive technology. This documentation is available in HTML format, and contains markup to facilitate access by the disabled community. Accessibility standards will continue to evolve over time, and Oracle is actively engaged with other market-leading technology vendors to address technical obstacles so that our documentation can be accessible to all of our customers. For more information, visit the Oracle Accessibility Program Web site at

Accessibility of Code Examples in Documentation

Screen readers may not always correctly read the code examples in this document. The conventions for writing code require that closing braces should appear on an otherwise empty line; however, some screen readers may not always read a line of text that consists solely of a bracket or brace.

Accessibility of Links to External Web Sites in Documentation

This documentation may contain links to Web sites of other companies or organizations that Oracle does not own or control. Oracle neither evaluates nor makes any representations regarding the accessibility of these Web sites.

TTY Access to Oracle Support Services

Oracle provides dedicated Text Telephone (TTY) access to Oracle Support Services within the United States of America 24 hours a day, seven days a week. For TTY support, call 800.446.2398.


This document contains:

Chapter 1, "Overview of PL/SQL"

Summarizes the main features of PL/SQL and their advantages. Introduces the basic concepts behind PL/SQL and the general appearance of PL/SQL programs.

Chapter 2, "Fundamentals of the PL/SQL Language"

Focuses on the small-scale aspects of PL/SQL, such as lexical units, scalar datatypes, user-defined subtypes, data conversion, expressions, assignments, block structure, declarations, and scope.

Chapter 3, "PL/SQL Datatypes"

Discusses PL/SQL's predefined datatypes, which include integer, floating-point, character, Boolean, date, collection, reference, and LOB types. Also discusses user-defined subtypes and data conversion.

Chapter 4, "Using PL/SQL Control Structures"

Shows how to control the flow of execution through a PL/SQL program. Describes conditional, iterative, and sequential control, with control structures such as IF-THEN-ELSE, CASE, and WHILE-LOOP.

Chapter 5, "Using PL/SQL Collections and Records"

Discusses the composite datatypes TABLE, VARRAY, and RECORD. You learn how to reference and manipulate whole collections of data and group data of different types together.

Chapter 6, "Performing SQL Operations from PL/SQL"

Shows how PL/SQL supports the SQL commands, functions, and operators for manipulating Oracle data. Also shows how to process queries and transactions.

Chapter 7, "Performing SQL Operations with Native Dynamic SQL"

Shows how to build SQL statements and queries at run time.

Chapter 8, "Using PL/SQL Subprograms"

Shows how to write and call procedures and functions. It discusses related topics such as parameters, overloading, and different privilege models for subprograms.

Chapter 9, "Using PL/SQL Packages"

Shows how to bundle related PL/SQL types, items, and subprograms into a package. Packages define APIs that can be reused by many applications.

Chapter 10, "Handling PL/SQL Errors"

Shows how to detect and handle PL/SQL errors using exceptions and handlers.

Chapter 11, "Tuning PL/SQL Applications for Performance"

Discusses how to improve performance for PL/SQL-based applications.

Chapter 12, "Using PL/SQL With Object Types"

Discusses how to manipulate objects through PL/SQL.

Chapter 13, "PL/SQL Language Elements"

Shows the syntax of statements, parameters, and other PL/SQL language elements. Also includes usage notes and links to examples in the book.

Appendix A, "Obfuscating PL/SQL Source Code"

Describes how to use the standalone wrap utility and subprograms of the DBMS_DDL package to obfuscate PL/SQL source code, enabling you to deliver PL/SQL applications without exposing your source code.

Appendix B, "How PL/SQL Resolves Identifier Names"

Explains how PL/SQL resolves references to names in potentially ambiguous SQL and procedural statements.

Appendix C, "PL/SQL Program Limits"

Explains the compile-time and runtime limits imposed by PL/SQL.

Appendix D, "PL/SQL Reserved Words and Keywords"

Lists the words that are reserved for use by PL/SQL.

PL/SQL Sample Programs

You can install the PL/SQL sample programs from the Oracle Database Companion CD. The demos are installed in the PL/SQL demo directory, typically ORACLE_HOME/plsql/demo. For the exact location of the directory, see the Oracle installation guide for your system. These samples are typically older ones based on the SCOTT schema, with its EMP and DEPT tables.

Most examples in this book have been made into complete programs that you can run under the HR sample schema, with its EMPLOYEES and DEPARTMENTS tables.

The Oracle Technology Network Web site has a PL/SQL section with many sample programs to download, at These programs demonstrate many language features, particularly the most recent ones. You can use some of the programs to compare performance of PL/SQL across database releases.

For examples of calling PL/SQL from other languages, see Oracle Database Java Developer's Guide and Pro*C/C++ Programmer's Guide.

Related Documents

For more information, see these Oracle resources:

Many of the examples in this book use the sample schemas, which are installed by default when you select the Basic Installation option with an Oracle Database installation. Refer to Oracle Database Sample Schemas for information on how these schemas were created and how you can use them yourself.

Printed documentation is available for sale in the Oracle Store at

To download free release notes, installation documentation, white papers, or other collateral, please visit the Oracle Technology Network (OTN). You must register online before using OTN; registration is free and can be done at

If you already have a username and password for OTN, then you can go directly to the documentation section of the OTN Web site at

For information on additional books


This section describes the conventions used in the text and code examples of this documentation set. It describes:

Conventions in Text

We use various conventions in text to help you more quickly identify special terms. The following table describes those conventions and provides examples of their use.

Convention Meaning Example
Bold Bold typeface indicates terms that are defined in the text or terms that appear in a glossary, or both. When you specify this clause, you create an index-organized table.
Italics Italic typeface indicates book titles or emphasis. Oracle Database Concepts

Ensure that the recovery catalog and target database do not reside on the same disk.

UPPERCASE monospace (fixed-width) font Uppercase monospace typeface indicates elements supplied by the system. Such elements include parameters, privileges, datatypes, Recovery Manager keywords, SQL keywords, SQL*Plus or utility commands, packages and methods, as well as system-supplied column names, database objects and structures, usernames, and roles. You can specify this clause only for a NUMBER column.

You can back up the database by using the BACKUP command.

Query the TABLE_NAME column in the USER_TABLES data dictionary view.


lowercase monospace (fixed-width) font Lowercase monospace typeface indicates executable programs, filenames, directory names, and sample user-supplied elements. Such elements include computer and database names, net service names and connect identifiers, user-supplied database objects and structures, column names, packages and classes, usernames and roles, program units, and parameter values.

Note: Some programmatic elements use a mixture of UPPERCASE and lowercase. Enter these elements as shown.

Enter sqlplus to start SQL*Plus.

The password is specified in the orapwd file.

Back up the datafiles and control files in the /disk1/oracle/dbs directory.

The department_id, department_name, and location_id columns are in the hr.departments table.

Set the QUERY_REWRITE_ENABLED initialization parameter to true.

Connect as oe user.

The JRepUtil class implements these methods.

lowercase italic monospace (fixed-width) font Lowercase italic monospace font represents placeholders or variables. You can specify the parallel_clause.

Run old_release.SQL where old_release refers to the release you installed prior to upgrading.

Conventions in Code Examples

Code examples illustrate SQL, PL/SQL, SQL*Plus, or other command-line statements. They are displayed in a monospace (fixed-width) font and separated from normal text as shown in this example:


The following table describes typographic conventions used in code examples and provides examples of their use.

Convention Meaning Example
[ ]
Anything enclosed in brackets is optional.
DECIMAL (digits [ , precision ])
{ }
Braces are used for grouping items.
A vertical bar represents a choice of two options.
Ellipsis points mean repetition in syntax descriptions.

In addition, ellipsis points can mean an omission in code examples or text.

CREATE TABLE ... AS subquery;

SELECT col1, col2, ... , coln FROM employees;
Other symbols You must use symbols other than brackets ([ ]), braces ({ }), vertical bars (|), and ellipsis points (...) exactly as shown.
acctbal NUMBER(11,2);
acct    CONSTANT NUMBER(4) := 3;
Italicized text indicates placeholders or variables for which you must supply particular values.
CONNECT SYSTEM/system_password
DB_NAME = database_name
Uppercase typeface indicates elements supplied by the system. We show these terms in uppercase in order to distinguish them from terms you define. Unless terms appear in brackets, enter them in the order and with the spelling shown. Because these terms are not case sensitive, you can use them in either UPPERCASE or lowercase.
SELECT last_name, employee_id FROM employees;
DROP TABLE hr.employees;
Lowercase typeface indicates user-defined programmatic elements, such as names of tables, columns, or files.

Note: Some programmatic elements use a mixture of UPPERCASE and lowercase. Enter these elements as shown.

SELECT last_name, employee_id FROM employees;
sqlplus hr/hr