1 Overview of PL/SQL

This chapter introduces the main features of the PL/SQL language. It shows how PL/SQL meets the challenges of database programming, and how you can reuse techniques that you know from other programming languages.

This chapter contains these topics:

See Also:

Advantages of PL/SQL


Tight Integration with SQL




Running a SQL query and processing the result set is as easy in PL/SQL as opening a text file and processing each line in popular scripting languages. Using PL/SQL to access metadata about database objects and handle database error conditions, you can write utility programs for database administration that are reliable and produce readable output about the success of each operation. Many database features, such as triggers and object types, make use of PL/SQL. You can write the bodies of triggers and methods for object types in PL/SQL.

PL/SQL supports both static and dynamic SQL. The syntax of static SQL statements is known at precompile time and the preparation of the static SQL occurs before runtime, where as the syntax of dynamic SQL statements is not known until runtime. Dynamic SQL is a programming technique that makes your applications more flexible and versatile. Your programs can build and process SQL data definition, data control, and session control statements at run time, without knowing details such as table names and WHERE clauses in advance. For information on the use of static SQL with PL/SQL, see Chapter 6, "Performing SQL Operations from PL/SQL". For information on the use of dynamic SQL, see Chapter 7, "Performing SQL Operations with Native Dynamic SQL". For additional information about dynamic SQL, see Oracle Database Application Developer's Guide - Fundamentals.

Better Performance




Figure 1-1 PL/SQL Boosts Performance

Description of lnpls005.gif follows
Description of the illustration lnpls005.gif

Higher Productivity

PL/SQL lets you write very compact code for manipulating data. In the same way that scripting languages such as Perl can read, transform, and write data from files, PL/SQL can query, transform, and update data in a database. PL/SQL saves time on design and debugging by offering a full range of software-engineering features, such as exception handling, encapsulation, data hiding, and object-oriented datatypes.

PL/SQL extends tools such as Oracle Forms. With PL/SQL in these tools, you can use familiar language constructs to build applications. For example, you can use an entire PL/SQL block in an Oracle Forms trigger, instead of multiple trigger steps, macros, or user exits. PL/SQL is the same in all environments. After you learn PL/SQL with one Oracle tool, you can transfer your knowledge to other tools.

Full Portability


Tight Security


For information on wrapping, or hiding, the source of a PL/SQL unit, see Appendix A, "Obfuscating PL/SQL Source Code".

Access to Pre-defined Packages

Oracle provides product-specific packages that define APIs you can call from PL/SQL to perform many useful tasks. These packages include DBMS_ALERT for using database triggers, DBMS_FILE for reading and writing operating system (OS) text files, DBMS_HTTP for making hypertext transfer protocol (HTTP) callouts, DBMS_OUTPUT for display output from PL/SQL blocks and subprograms, and DBMS_PIPE for communicating over named pipes. For additional information on these packages, see "Overview of Product-Specific Packages".

For complete information on the packages supplied by Oracle, see Oracle Database PL/SQL Packages and Types Reference.

Support for Object-Oriented Programming


By encapsulating operations with data, object types let you move data-maintenance code out of SQL scripts and PL/SQL blocks into methods. Also, object types hide implementation details, so that you can change the details without affecting client programs. See Chapter 12, "Using PL/SQL With Object Types".

In addition, object types allow for realistic data modeling. Complex real-world entities and relationships map directly into object types. This direct mapping helps your programs better reflect the world they are trying to simulate. For information on object types, see Oracle Database Application Developer's Guide - Object-Relational Features.

Support for Developing Web Applications and Pages

You can use PL/SQL to develop Web applications and Server Pages (PSPs). For an overview of the use of PL/SQL with the Web, see "Using PL/SQL to Create Web Applications and Server Pages".

Understanding the Main Features of PL/SQL

PL/SQL combines the data-manipulating power of SQL with the processing power of procedural languages. You can control program flow with statements like IF and LOOP. As with other procedural programming languages, you can declare variables, define procedures and functions, and trap runtime errors.


Understanding PL/SQL Block Structure

The basic units (procedures, functions, and anonymous blocks) that make up a PL/SQL program are logical blocks, which can be nested inside one another. A block groups related declarations and statements. You can place declarations close to where they are used, such as inside a large subprogram. The declarations are local to the block and cease to exist when the block completes, helping to avoid cluttered namespaces for variables and procedures. For a syntax description of the block structure, see "Block Declaration".

As Figure 1-2 shows, a PL/SQL block has three basic parts: a declarative part (DECLARE), an executable part (BEGIN .. END), and an exception-handling (EXCEPTION) part that handles error conditions. Only the executable part is required. The optional declarative part is written first, where you define types, variables, and similar items. These items are manipulated in the executable part. Exceptions raised during execution can be dealt with in the exception-handling part. For an example of PL/SQL block structure, see Example 1-3.


Understanding PL/SQL Variables and Constants

PL/SQL lets you declare variables and constants, then use them in SQL and procedural statements anywhere an expression can be used. You must declare a constant or variable before referencing it in any other statements. For additional information, see "Declarations".

Declaring Variables

Variables can have any SQL datatype, such as CHAR, DATE, or NUMBER, or a PL/SQL-only datatype, such as BOOLEAN or PLS_INTEGER. For example, assume that you want to declare variables for part data, such as part_no to hold 6-digit numbers and in_stock to hold the Boolean value TRUE or FALSE. You declare these and related part variables as shown in Example 1-1. Note that there is a semi-colon (;) at the end of each line in the declaration section.

Example 1-1 Declaring Variables in PL/SQL

  part_no    NUMBER(6);
  part_name  VARCHAR2(20);
  in_stock   BOOLEAN;
  part_price NUMBER(6,2);
  part_desc  VARCHAR2(50);

You can also declare nested tables, variable-size arrays (varrays for short), and records using the TABLE, VARRAY, and RECORD composite datatypes. See Chapter 5, "Using PL/SQL Collections and Records".

Assigning Values to a Variable

You can assign values to a variable in three ways. The first way uses the assignment operator (:=), a colon followed by an equal sign, as shown in Example 1-2. You place the variable to the left of the operator and an expression, including function calls, to the right. Note that you can assign a value to a variable when it is declared.

Example 1-2 Assigning Values to Variables With the Assignment Operator

   wages          NUMBER;
   hours_worked   NUMBER := 40;
   hourly_salary  NUMBER := 22.50;
   bonus          NUMBER := 150;
   country        VARCHAR2(128);
   counter        NUMBER := 0;
   done           BOOLEAN;
   valid_id       BOOLEAN;
   emp_rec1       employees%ROWTYPE;
   emp_rec2       employees%ROWTYPE;
   comm_tab       commissions;
   wages := (hours_worked * hourly_salary) + bonus;
   country := 'France';
   country := UPPER('Canada');
   done := (counter > 100);
   valid_id := TRUE;
   emp_rec1.first_name := 'Antonio';
   emp_rec1.last_name := 'Ortiz';
   emp_rec1 := emp_rec2;
   comm_tab(5) := 20000 * 0.15;

The second way to assign values to a variable is by selecting (or fetching) database values into it. In Example 1-3, 10% of an employee's salary is selected into the bonus variable. Now you can use the bonus variable in another computation or insert its value into a database table.

Example 1-3 Assigning Values to Variables by SELECTing INTO

  bonus  NUMBER(8,2);
  emp_id NUMBER(6) := 100;
  SELECT salary * 0.10 INTO bonus FROM employees 
    WHERE employee_id = emp_id;

The third way to assign a value to a variable is by passing it as an OUT or IN OUT parameter to a subprogram, and then assigning the value inside the subprogram. Example 1-4 passes the sal variable to a subprogram, and the subprogram updates the variable.

In the example, DBMS_OUTPUT.PUT_LINE is used to display output from the PL/SQL program. For more information, see "Inputting and Outputting Data with PL/SQL". For information on the DBMS_OUTPUT package, see "About the DBMS_OUTPUT Package".

Example 1-4 Assigning Values to Variables as Parameters of a Subprogram

REM SERVEROUTPUT must be set to ON to display output with DBMS_OUTPUT
  new_sal NUMBER(8,2);
  emp_id  NUMBER(6) := 126;
  PROCEDURE adjust_salary(emp_id NUMBER, sal IN OUT NUMBER) IS  
    emp_job VARCHAR2(10);
    avg_sal NUMBER(8,2);
      SELECT job_id INTO emp_job FROM employees WHERE employee_id = emp_id;
      SELECT AVG(salary) INTO avg_sal FROM employees WHERE job_id = emp_job;
      DBMS_OUTPUT.PUT_LINE ('The average salary for ' || emp_job 
                            || ' employees: ' || TO_CHAR(avg_sal));
      sal := (sal + avg_sal)/2; -- adjust sal value which is returned
  SELECT AVG(salary) INTO new_sal FROM employees;
  DBMS_OUTPUT.PUT_LINE ('The average salary for all employees: ' 
                        || TO_CHAR(new_sal));
  adjust_salary(emp_id, new_sal); -- assigns a new value to new_sal
  DBMS_OUTPUT.PUT_LINE ('The adjusted salary for employee ' || TO_CHAR(emp_id) 
                        || ' is ' || TO_CHAR(new_sal)); -- sal has new value

Bind Variables



'DELETE FROM employees WHERE employee_id = :id' USING emp_id;

For an example of the use of bind variables, see Example 7-1.

Declaring Constants


credit_limit CONSTANT NUMBER := 5000.00;

See "Constants".

Processing Queries with PL/SQL


Example 1-5 Processing Query Results in a LOOP

    DBMS_OUTPUT.PUT_LINE('First name = ' || someone.first_name ||
                         ', Last name = ' || someone.last_name);


Declaring PL/SQL Subprograms

Subprograms are named PL/SQL blocks that can be called with a set of parameters. PL/SQL has two types of subprograms: procedures and functions. The following is an example of a declaration of a PL/SQL procedure:

  in_string  VARCHAR2(100) := 'This is my test string.';
  out_string VARCHAR2(200);
  PROCEDURE double ( original IN VARCHAR2, new_string OUT VARCHAR2 ) AS
      new_string := original || original;

For example of a subprogram declaration in a package, see Example 1-13. For more information on subprograms, see "What Are Subprograms?".

You can create standalone subprograms with SQL statements that are stored in the database. See Subprograms: Procedures and Functions.

Declaring Datatypes for PL/SQL Variables

As part of the declaration for each PL/SQL variable, you declare its datatype. Usually, this datatype is one of the types shared between PL/SQL and SQL, such as NUMBER or VARCHAR2. For easier maintenance of code that interacts with the database, you can also use the special qualifiers %TYPE and %ROWTYPE to declare variables that hold table columns or table rows. For more information on datatypes, see Chapter 3, "PL/SQL Datatypes".


The %TYPE attribute provides the datatype of a variable or database column. This is particularly useful when declaring variables that will hold database values. For example, assume there is a column named last_name in a table named employees. To declare a variable named v_last_name that has the same datatype as column title, use dot notation and the %TYPE attribute, as follows:

v_last_name employees.last_name%TYPE;

Declaring v_last_name with %TYPE has two advantages. First, you need not know the exact datatype of last_name. Second, if you change the database definition of last_name, perhaps to make it a longer character string, the datatype of v_last_name changes accordingly at run time.

For more information on %TYPE, see "Using the %TYPE Attribute" and "%TYPE Attribute".



Columns in a row and corresponding fields in a record have the same names and datatypes. In the following example, you declare a record named dept_rec. Its fields have the same names and datatypes as the columns in the departments table.

  dept_rec departments%ROWTYPE; -- declare record variable


v_deptid := dept_rec.department_id;

If you declare a cursor that retrieves the last name, salary, hire date, and job class of an employee, you can use %ROWTYPE to declare a record that stores the same information as shown in Example 1-6. When you execute the FETCH statement, the value in the last_name column of the employees table is assigned to the last_name field of employee_rec, the value in the salary column is assigned to the salary field, and so on.

Example 1-6 Using %ROWTYPE with an Explicit Cursor

    SELECT last_name, salary, hire_date, job_id FROM employees 
       WHERE employee_id = 120;
-- declare record variable that represents a row fetched from the employees table
   employee_rec c1%ROWTYPE; 
-- open the explicit cursor and use it to fetch data into employee_rec
  OPEN c1;
  FETCH c1 INTO employee_rec;
  DBMS_OUTPUT.PUT_LINE('Employee name: ' || employee_rec.last_name);

For more information on %ROWTYPE, see "Using the %ROWTYPE Attribute" and "%ROWTYPE Attribute".

Understanding PL/SQL Control Structures

Control structures are the most important PL/SQL extension to SQL. Not only does PL/SQL let you manipulate Oracle data, it lets you process the data using conditional, iterative, and sequential flow-of-control statements such as IF-THEN-ELSE, CASE, FOR-LOOP, WHILE-LOOP, EXIT-WHEN, and GOTO. For additional information, see Chapter 4, "Using PL/SQL Control Structures".

Conditional Control

Often, it is necessary to take alternative actions depending on circumstances. The IF-THEN-ELSE statement lets you execute a sequence of statements conditionally. The IF clause checks a condition, the THEN clause defines what to do if the condition is true and the ELSE clause defines what to do if the condition is false or null. Example 1-7 shows the use of IF-THEN-ELSE to determine the salary raise an employee receives based on the current salary of the employee.

To choose among several values or courses of action, you can use CASE constructs. The CASE expression evaluates a condition and returns a value for each case. The case statement evaluates a condition and performs an action, such as an entire PL/SQL block, for each case. See Example 1-7.

Example 1-7 Using the IF-THEN_ELSE and CASE Statement for Conditional Control

   jobid      employees.job_id%TYPE;
   empid      employees.employee_id%TYPE := 115;
   sal        employees.salary%TYPE;
   sal_raise  NUMBER(3,2);
  SELECT job_id, salary INTO jobid, sal from employees WHERE employee_id = empid;
    WHEN jobid = 'PU_CLERK' THEN
        IF sal < 3000 THEN sal_raise := .12;
          ELSE sal_raise := .09;
        END IF;
    WHEN jobid = 'SH_CLERK' THEN
        IF sal < 4000 THEN sal_raise := .11;
          ELSE sal_raise := .08;
        END IF;
    WHEN jobid = 'ST_CLERK' THEN
        IF sal < 3500 THEN sal_raise := .10;
          ELSE sal_raise := .07;
        END IF;
       DBMS_OUTPUT.PUT_LINE('No raise for this job: ' || jobid);
  UPDATE employees SET salary = salary + salary * sal_raise 
    WHERE employee_id = empid;


Iterative Control


  -- sequence of statements

The FOR-LOOP statement lets you specify a range of integers, then execute a sequence of statements once for each integer in the range. In Example 1-8 the loop inserts 100 numbers, square roots, squares, and the sum of squares into a database table:

Example 1-8 Using the FOR-LOOP

CREATE TABLE sqr_root_sum (num NUMBER, sq_root NUMBER(6,2),
                           sqr NUMBER, sum_sqrs NUMBER);
  FOR i in 1..100 LOOP
    s := (i * (i + 1) * (2*i +1)) / 6; -- sum of squares
    INSERT INTO sqr_root_sum VALUES (i, SQRT(i), i*i, s );


In Example 1-9, you find the first employee who has a salary over $15000 and is higher in the chain of command than employee 120:

Example 1-9 Using WHILE-LOOP for Control

CREATE TABLE temp (tempid NUMBER(6), tempsal NUMBER(8,2), tempname VARCHAR2(25));
   sal            employees.salary%TYPE := 0;
   mgr_id         employees.manager_id%TYPE;
   lname          employees.last_name%TYPE;
   starting_empid employees.employee_id%TYPE := 120;
   SELECT manager_id INTO mgr_id FROM employees 
      WHERE employee_id = starting_empid;
   WHILE sal <= 15000 LOOP -- loop until sal > 15000
      SELECT salary, manager_id, last_name INTO sal, mgr_id, lname
         FROM employees WHERE employee_id = mgr_id;
   INSERT INTO temp VALUES (NULL, sal, lname); -- insert NULL for tempid
      INSERT INTO temp VALUES (NULL, NULL, 'Not found'); -- insert NULLs

The EXIT-WHEN statement lets you complete a loop if further processing is impossible or undesirable. When the EXIT statement is encountered, the condition in the WHEN clause is evaluated. If the condition is true, the loop completes and control passes to the next statement. In Example 1-10, the loop completes when the value of total exceeds 25,000:

Example 1-10 Using the EXIT-WHEN Statement

  total   NUMBER(9) := 0;
  counter NUMBER(6) := 0;
    counter := counter + 1;
    total := total + counter * counter;
    -- exit loop when condition is true
    EXIT WHEN total > 25000;  
  DBMS_OUTPUT.PUT_LINE('Counter: ' || TO_CHAR(counter) || ' Total: ' || TO_CHAR(total));

Sequential Control


Example 1-11 Using the GOTO Statement

  total   NUMBER(9) := 0;
  counter NUMBER(6) := 0;
    counter := counter + 1;
    total := total + counter * counter;
    -- branch to print_total label when condition is true
    IF total > 25000 THEN GOTO print_total;
      ELSE GOTO calc_total;
    END IF;

Understanding Conditional Compilation

Using conditional compilation, you can customize the functionality in a compiled PL/SQL application by conditionalizing functionality rather than removing any source code. For example, conditional compilation enables you to determine which PL/SQL features in a PL/SQL application are used for specific database releases. The latest PL/SQL features in an application can be run on a new database release while at the same time those features can be conditionalizing so that the same application is compatible with a previous database release. Conditional compilation is also useful when you want to execute debugging procedures in a development environment, but want to turn off the debugging routines in a production environment. See "Conditional Compilation".

Writing Reusable PL/SQL Code

PL/SQL lets you break an application down into manageable, well-defined modules. PL/SQL meets this need with program units, which include blocks, subprograms, and packages. You can reuse program units by loading them into the database as triggers, stored procedures, and stored functions. For additional information, see Chapter 8, "Using PL/SQL Subprograms" and Chapter 9, "Using PL/SQL Packages".

Subprograms: Procedures and Functions

There are two types of subprograms called procedures and functions, which can accept parameters and be invoked (called). See "What Are Subprograms?".

The SQL CREATE PROCEDURE statement lets you create standalone procedures that are stored in the database. For information, see CREATE PROCEDURE in Oracle Database SQL Reference. The SQL CREATE FUNCTION statement lets you create standalone functions that are stored in an Oracle database. For information, see CREATE FUNCTION in Oracle Database SQL Reference. These stored (schema level) subprograms can be accessed from SQL.

As shown in Example 1-12, a subprogram is like a miniature program, beginning with a header followed by an optional declarative part, an executable part, and an optional exception-handling part.

Example 1-12 Creating a Stored Subprogram

-- including OR REPLACE is more convenient when updating a subprogram
   commission        REAL;
   comm_missing EXCEPTION;
BEGIN  -- executable part starts here
   SELECT commission_pct / 100 INTO commission FROM employees
    WHERE employee_id = emp_id;
   IF commission IS NULL THEN
      RAISE comm_missing;
      UPDATE employees SET salary = salary + bonus*commission 
      WHERE employee_id = emp_id;
   END IF;
EXCEPTION  -- exception-handling part starts here
   WHEN comm_missing THEN
      DBMS_OUTPUT.PUT_LINE('This employee does not receive a commission.');
      commission := 0;
      NULL; -- for other exceptions do nothing
END award_bonus;
CALL award_bonus(150, 400);

When called, this procedure accepts an employee Id and a bonus amount. It uses the Id to select the employee's commission percentage from a database table and, at the same time, convert the commission percentage to a decimal amount. Then, it checks the commission amount. If the commission is null, an exception is raised; otherwise, the employee's salary is updated.

Packages: APIs Written in PL/SQL




In Example 1-13, the emp_actions package contain two procedures that update the employees table and one function that provides information.

Example 1-13 Creating a Package and Package Body

CREATE OR REPLACE PACKAGE emp_actions AS  -- package specification
   PROCEDURE hire_employee (employee_id NUMBER, last_name VARCHAR2, 
     first_name VARCHAR2, email VARCHAR2, phone_number VARCHAR2,
    hire_date DATE, job_id VARCHAR2, salary NUMBER, commission_pct NUMBER,
    manager_id NUMBER, department_id NUMBER);
   PROCEDURE fire_employee (emp_id NUMBER);
   FUNCTION num_above_salary (emp_id NUMBER) RETURN NUMBER;
END emp_actions;
CREATE OR REPLACE PACKAGE BODY emp_actions AS  -- package body
-- code for procedure hire_employee
   PROCEDURE hire_employee (employee_id NUMBER, last_name VARCHAR2,
      first_name VARCHAR2, email VARCHAR2, phone_number VARCHAR2, hire_date DATE,
      job_id VARCHAR2, salary NUMBER, commission_pct NUMBER,
      manager_id NUMBER, department_id NUMBER) IS
      INSERT INTO employees VALUES (employee_id, last_name, first_name, email,
       phone_number, hire_date, job_id, salary, commission_pct, manager_id,
   END hire_employee;
-- code for procedure fire_employee
   PROCEDURE fire_employee (emp_id NUMBER) IS
      DELETE FROM employees WHERE employee_id = emp_id;
   END fire_employee;
-- code for function num_above salary
   FUNCTION num_above_salary (emp_id NUMBER) RETURN NUMBER IS
      emp_sal NUMBER(8,2);
      num_count NUMBER;
      SELECT salary INTO emp_sal FROM employees WHERE employee_id = emp_id;
      SELECT COUNT(*) INTO num_count FROM employees WHERE salary > emp_sal;
      RETURN num_count;
   END num_above_salary;
END emp_actions;

Applications that call these procedures only need to know the names and parameters from the package specification. You can change the implementation details inside the package body without affecting the calling applications.

To call the procedures of the emp_actions package created in Example 1-13, you can execute the statements in Example 1-14. The procedures can be executed in a BEGIN .. END block or with the SQL CALL statement. Note the use of the package name as a prefix to the procedure name.

Example 1-14 Calling a Procedure in a Package

CALL emp_actions.hire_employee(300, 'Belden', 'Enrique', 'EBELDEN',
   '555.111.2222', '31-AUG-04', 'AC_MGR', 9000, .1, 101, 110);

  DBMS_OUTPUT.PUT_LINE( 'Number of employees with higher salary: ' || 

Packages are stored in the database, where they can be shared by many applications. Calling a packaged subprogram for the first time loads the whole package and caches it in memory, saving on disk I/O for subsequent calls. Thus, packages enhance reuse and improve performance in a multiuser, multi-application environment. For information on packages, see Chapter 9, "Using PL/SQL Packages".

If a subprogram does not take any parameters, you can include an empty set of parentheses or omit the parentheses, both in PL/SQL and in functions called from SQL queries. For calls to a method that takes no parameters, an empty set of parentheses is optional within PL/SQL scopes but required within SQL scopes.

Inputting and Outputting Data with PL/SQL


SQL*Plus does not display DBMS_OUTPUT data unless you first issue the SQL*Plus command SET SERVEROUTPUT ON as follows:


For information on the SEVEROUTPUT setting, see the "SQL*Plus Command Reference" chapter in SQL*Plus User's Guide and Reference.

Other PL/SQL APIs for processing I/O are:

  • HTF and HTP for displaying output on a web page

  • DBMS_PIPE for passing information back and forth between PL/SQL and operating-system commands

  • UTL_FILE for reading and writing operating-system files

  • UTL_HTTP for communicating with web servers

  • UTL_SMTP for communicating with mail servers

See "Overview of Product-Specific Packages". Although some of these APIs can accept input as well as output, there is no built-in language facility for accepting data directly from the keyboard. For that, you can use the PROMPT and ACCEPT commands in SQL*Plus.

Understanding PL/SQL Data Abstraction

Data abstraction lets you work with the essential properties of data without being too involved with details. After you design a data structure, you can focus on designing algorithms that manipulate the data structure.


A cursor is a name for a specific private SQL area in which information for processing the specific statement is kept. PL/SQL uses both implicit and explicit cursors. PL/SQL implicitly declares a cursor for all SQL data manipulation statements on a set of rows, including queries that return only one row. For queries that return more than one row, you can explicitly declare a cursor to process the rows individually. For example, Example 1-6 declares an explicit cursor.

For information on managing cursors with PL/SQL, see "Managing Cursors in PL/SQL".


PL/SQL collection types let you declare high-level datatypes similar to arrays, sets, and hash tables found in other languages. In PL/SQL, array types are known as varrays (short for variable-size arrays), set types are known as nested tables, and hash table types are known as associative arrays. Each kind of collection is an ordered group of elements, all of the same type. Each element has a unique subscript that determines its position in the collection. When declaring collections, you use a TYPE definition. See "Defining Collection Types and Declaring Collection Variables".

To reference an element, use subscript notation with parentheses, as shown in Example 1-15.

Example 1-15 Using a PL/SQL Collection Type

  TYPE staff_list IS TABLE OF employees.employee_id%TYPE;
  staff staff_list;
  lname employees.last_name%TYPE;
  fname employees.first_name%TYPE;
   staff := staff_list(100, 114, 115, 120, 122);
   FOR i IN staff.FIRST..staff.LAST LOOP
     SELECT last_name, first_name INTO lname, fname FROM employees 
       WHERE employees.employee_id = staff(i);
     DBMS_OUTPUT.PUT_LINE ( TO_CHAR(staff(i)) || ': ' || lname || ', ' || fname );


For information on collections, see Chapter 5, "Using PL/SQL Collections and Records".


Records are composite data structures whose fields can have different datatypes. You can use records to hold related items and pass them to subprograms with a single parameter. When declaring records, you use a TYPE definition. See "Defining and Declaring Records".

Example 1-16 shows how are records are declared.

Example 1-16 Declaring a Record Type

   TYPE timerec IS RECORD (hours SMALLINT, minutes SMALLINT);
   TYPE meetin_typ IS RECORD (
      date_held DATE,
      duration  timerec,  -- nested record
      location  VARCHAR2(20),
      purpose   VARCHAR2(50));
-- NULL does nothing but allows unit to be compiled and tested


For information on records, see Chapter 5, "Using PL/SQL Collections and Records".

Object Types


Object types reduce complexity by breaking down a large system into logical entities. This lets you create software components that are modular, maintainable, and reusable. Object-type definitions, and the code for the methods, are stored in the database. Instances of these object types can be stored in tables or used as variables inside PL/SQL code. Example 1-17 shows an object type definition for a bank account.

Example 1-17 Defining an Object Type

CREATE TYPE bank_account AS OBJECT ( 
  acct_number NUMBER(5),
  balance     NUMBER,
  status      VARCHAR2(10),
  MEMBER PROCEDURE open (SELF IN OUT NOCOPY bank_account, amount IN NUMBER),
                          amount OUT NUMBER),
                            amount IN NUMBER),
  MEMBER PROCEDURE withdraw (SELF IN OUT NOCOPY bank_account, num IN NUMBER,
                             amount IN NUMBER),

For information on object types, see Oracle Database Application Developer's Guide - Object-Relational Features. For information on the use of PL/SQL with objects, see Chapter 12, "Using PL/SQL With Object Types".

Understanding PL/SQL Error Handling


PL/SQL's exception handling is different from the manual checking you might be used to from C programming, where you insert a check to make sure that every operation succeeded. Instead, the checks and calls to error routines are performed automatically, similar to the exception mechanism in Java programming.


You can declare exceptions of your own, for conditions that you decide are errors, or to correspond to database errors that normally result in ORA- error messages. When you detect a user-defined error condition, you execute a RAISE statement. See the exception comm_missing in Example 1-12. In the example, if the commission is null, the exception comm_missing is raised.


For information on PL/SQL errors, see "Overview of PL/SQL Runtime Error Handling". For information on PL/SQL warnings, see "Overview of PL/SQL Compile-Time Warnings".

PL/SQL Architecture



In the Oracle Database Server


Anonymous Blocks


Stored Subprograms




Stored subprograms defined within a package are known as packaged subprograms. Those defined independently are called standalone subprograms. Subprograms nested inside other subprograms or within a PL/SQL block are known as local subprograms, which cannot be referenced by other applications and exist only inside the enclosing block.



Example 1-18 shows how you can call the stored subprogram in Example 1-12 from SQL*Plus using the CALL statement or using a BEGIN ... END block.

Example 1-18 Techniques for Calling a Standalone Procedure From SQL*Plus

CALL award_bonus(179, 1000);

  award_bonus(179, 10000);
-- using named notation
BEGIN award_bonus(emp_id=>179, bonus=>10000); END;

Using the BEGIN .. END block is recommended in several situations. Calling the subprogram from a BEGIN .. END block allows named or mixed notation for parameters which the CALL statement does not support. For information on named parameters, see "Using Positional, Named, or Mixed Notation for Subprogram Parameters". In addition, using the CALL statement can suppress an ORA-01403: no data found error that has not been handled in the PL/SQL subprogram.

For additional examples on calling PL/SQL procedures, see Example 8-5, "Subprogram Calls Using Positional, Named, and Mixed Notation" and "Passing Schema Object Names As Parameters". For information on the use of the CALL statement, see Oracle Database SQL Reference

Database Triggers

A database trigger is a stored subprogram associated with a database table, view, or event. The trigger can be called once, when some event occurs, or many times, once for each row affected by an INSERT, UPDATE, or DELETE statement. The trigger can be called after the event, to record it or take some followup action. Or, the trigger can be called before the event to prevent erroneous operations or fix new data so that it conforms to business rules. In Example 1-19 the table-level trigger fires whenever salaries in the employees table are updated, such as the processing in Example 1-7. For each update, the trigger writes a record to the emp_audit table.

Example 1-19 Creating a Database Trigger

CREATE TABLE emp_audit ( emp_audit_id NUMBER(6), up_date DATE, 
                         new_sal NUMBER(8,2), old_sal NUMBER(8,2) );

   AFTER UPDATE OF salary ON employees FOR EACH ROW
-- bind variables are used here for values
   INSERT INTO emp_audit VALUES( :old.employee_id, SYSDATE, 
                                 :new.salary, :old.salary );

The executable part of a trigger can contain procedural statements as well as SQL data manipulation statements. Besides table-level triggers, there are instead-of triggers for views and system-event triggers for schemas. For more information on triggers, see Oracle Database Concepts and Oracle Database Application Developer's Guide - Fundamentals. For information on the CREATE TRIGGER SQL statement, see Oracle Database SQL Reference.

In Oracle Tools

