PL/SQL User's Guide and Reference
Release 8.1.5






Prev Next


PL/SQL, Oracle's procedural extension to SQL, is an advanced fourth-generation programming language (4GL). It offers modern features such as data encapsulation, overloading, collection types, exception handling, and information hiding. PL/SQL also offers seamless SQL access, tight integration with the Oracle server and tools, portability, and security.

This guide explains all the concepts behind PL/SQL and illustrates every facet of the language. Good programming style is stressed throughout and supported by numerous examples. Using this guide, you learn PL/SQL quickly and effectively.

Major Topics
How This Guide Is Organized
Notational Conventions
Sample Database Tables


Anyone developing PL/SQL-based applications for Oracle8i will benefit from reading this guide. Written especially for programmers, this comprehensive treatment of PL/SQL will also be of value to systems analysts, project managers, and others interested in database applications. To use this guide effectively, you need a working knowledge of Oracle8i, SQL, and a 3GL such as Ada, C, or COBOL.

You will not find installation instructions or system-specific information in this guide. For that kind of information, see the Oracle installation or user's guide for your system.

How This Guide Is Organized

The PL/SQL User's Guide and Reference has 11 chapters and 5 appendices. Chapters 1 through 10 introduce you to PL/SQL and show you how to use its many features. Chapter 11 serves as a reference to PL/SQL commands, syntax, and semantics. Appendices A through E provide sample programs, supplementary technical information, and a list of reserved words.

Chapter 1, "Overview"

This chapter surveys the main features of PL/SQL and points out the advantages they offer. It also acquaints you with the basic concepts behind PL/SQL and the general appearance of PL/SQL programs.

Chapter 2, "Fundamentals"

This chapter focuses on the small-scale aspects of PL/SQL. It discusses lexical units, scalar datatypes, user-defined subtypes, data conversion, expressions, assignments, block structure, declarations, and scope.

Chapter 3, "Control Structures"

This chapter shows you how to structure the flow of control through a PL/SQL program. It describes conditional, iterative, and sequential control. You learn how to apply simple but powerful control structures such as IF-THEN-ELSE and WHILE-LOOP.

Chapter 4, "Collections and Records"

This chapter focuses on the composite datatypes TABLE, VARRAY, and RECORD. You learn how to reference and manipulate whole collections of data, and how to treat related but dissimilar data as a logical unit. You also learn how to improve performance by bulk-binding collections.

Chapter 5, "Interaction with Oracle"

This chapter shows you how PL/SQL supports the SQL commands, functions, and operators that let you manipulate Oracle data. You also learn how to manage cursors, process transactions, and safeguard your database.

Chapter 6, "Error Handling"

This chapter provides an in-depth discussion of error reporting and recovery. You learn how to detect and handle errors using PL/SQL exceptions.

Chapter 7, "Subprograms"

This chapter shows you how to write and use subprograms. It discusses procedures, functions, forward declarations, actual and formal parameters, positional and named notation, parameter modes, the NOCOPY compiler hint, parameter default values, aliasing, overloading, invoker rights, and recursion.

Chapter 8, "Packages"

This chapter shows you how to bundle related PL/SQL types, items, and subprograms into a package. Once written, your general-purpose package is compiled, then stored in an Oracle database, where its contents can be shared by many applications.

Chapter 9, "Object Types"

This chapter introduces you to object-oriented programming based on object types, which provide abstract templates for real-world objects. You learn how to define object types and manipulate objects.

Chapter 10, "Native Dynamic SQL"

This chapter shows you how to use dynamic SQL, an advanced programming technique that makes your applications more flexible and versatile. You learn two simple ways to write programs that can build and process SQL statements "on the fly" at run time.

Chapter 11, "Language Elements"

This chapter uses syntax diagrams to show how commands, parameters, and other language elements are sequenced to form PL/SQL statements. Also, it provides usage notes and short examples to help you become fluent in PL/SQL quickly.

Appendix A, "Sample Programs"

This appendix provides several PL/SQL programs to guide you in writing your own. The sample programs illustrate important concepts and features.

Appendix B, "CHAR versus VARCHAR2 Semantics"

This appendix explains the subtle but important semantic differences between the CHAR and VARCHAR2 base types.

Appendix C, "PL/SQL Wrapper"

This appendix shows you how to run the PL/SQL Wrapper, a stand-alone utility that enables you to deliver PL/SQL applications without exposing your source code.

Appendix D, "Name Resolution"

Thus appendix explains how PL/SQL resolves references to names in potentially ambiguous SQL and procedural statements.

Appendix E, "Reserved Words"

This appendix lists those words reserved for use by PL/SQL.

Notational Conventions

This guide follows these conventions:

Convention   Meaning  


Italic font denotes terms being defined for the first time, words being emphasized, error messages, and book titles.  


Courier font denotes PL/SQL code, keywords, program names, file names, and path names.  

PL/SQL code examples follow these conventions:

Convention   Meaning  


A double hyphen begins a single-line comment, which extends to the end of a line.  

/* */  

A slash-asterisk and an asterisk-slash delimit a multi-line comment, which can span multiple lines.  


An ellipsis shows that statements or clauses irrelevant to the discussion were left out.  

lower case  

Lower case is used for names of constants, variables, cursors, exceptions, subprograms, and packages.  


Upper case is used for keywords, names of predefined exceptions, and names of supplied PL/SQL packages.  

Mixed Case  

Mixed case is used for names of user-defined datatypes and subtypes. The names of user-defined types begin with an upper-case letter.  

Syntax definitions use a simple variant of Backus-Naur Form (BNF) that includes the following symbols:

Symbol   Meaning  

[ ]  

Brackets enclose optional items.  

{ }  

Braces enclose items only one of which is required.  


A vertical bar separates alternatives within brackets or braces.  


An ellipsis shows that the preceding syntactic element can be repeated.  


Delimiters other than brackets, braces, vertical bars, and ellipses must be entered as shown.  

Sample Database Tables

Most programming examples in this guide use two sample database tables named dept and emp. Their definitions follow:

   deptno NUMBER(2) NOT NULL,
   dname  VARCHAR2(14),
   loc    VARCHAR2(13));

   empno    NUMBER(4) NOT NULL,
   ename    VARCHAR2(10),
   job      VARCHAR2(9),
   mgr      NUMBER(4),
   hiredate DATE,
   sal      NUMBER(7,2),
   comm     NUMBER(7,2),
   deptno   NUMBER(2));

Sample Data

Respectively, the dept and emp tables contain the following rows of data:

------- ---------- ---------
30      SALES      CHICAGO

----- ------- --------- ------ --------- ------ ------ -------
 7369 SMITH   CLERK       7902 17-DEC-80    800             20
 7499 ALLEN   SALESMAN    7698 20-FEB-81   1600    300      30
 7521 WARD    SALESMAN    7698 22-FEB-81   1250    500      30
 7566 JONES   MANAGER     7839 02-APR-81   2975             20
 7654 MARTIN  SALESMAN    7698 28-SEP-81   1250   1400      30
 7698 BLAKE   MANAGER     7839 01-MAY-81   2850             30
 7782 CLARK   MANAGER     7839 09-JUN-81   2450             10
 7788 SCOTT   ANALYST     7566 19-APR-87   3000             20
 7839 KING    PRESIDENT        17-NOV-81   5000             10
 7844 TURNER  SALESMAN    7698 08-SEP-81   1500             30
 7876 ADAMS   CLERK       7788 23-MAY-87   1100             20
 7900 JAMES   CLERK       7698 03-DEC-81    950             30
 7902 FORD    ANALYST     7566 03-DEC-81   3000             20
 7934 MILLER  CLERK       7782 23-JAN-82   1300             10

Your Comments Are Welcome

We in Information Development appreciate your comments and suggestions. In fact, your opinions are the most important feedback we receive. We encourage you to use the Reader's Comment Form at the front of this guide. You can also send comments to us by


Copyright © 1999 Oracle Corporation.

All Rights Reserved.