Procedure Declaration

A procedure is a subprogram that can take parameters and be called. Generally, you use a procedure to perform an action. A procedure has two parts: the specification and the body. The specification (spec for short) begins with the keyword PROCEDURE and ends with the procedure name or a parameter list. Parameter declarations are optional. Procedures that take no parameters are written without parentheses. The procedure body begins with the keyword IS (or AS) and ends with the keyword END followed by an optional procedure name.

The procedure body has three parts: an optional declarative part, an executable part, and an optional exception-handling part. The declarative part contains declarations of types, cursors, constants, variables, exceptions, and subprograms. These items are local and cease to exist when you exit the procedure. The executable part contains statements that assign values, control execution, and manipulate Oracle data. The exception-handling part contains handlers that deal with exceptions raised during execution. For more information, see "Understanding PL/SQL Procedures". For an example of a procedure declaration, see Example 9-3.

Note that the procedure declaration in a PL/SQL block or package is not the same as creating a procedure in SQL. For information on the CREATE PROCEDURE SQL statement, see Oracle Database SQL Reference.


procedure specification ::=

Description of procedure_specification.gif follows
Description of the illustration procedure_specification.gif

procedure declaration ::=

Description of procedure_declaration.gif follows
Description of the illustration procedure_declaration.gif

procedure body ::=

Description of procedure_body.gif follows
Description of the illustration procedure_body.gif

parameter declaration ::=

Description of parameter_declaration.gif follows
Description of the illustration parameter_declaration.gif

Keyword and Parameter Description


A type specifier. For the syntax of datatype, see "Constant and Variable Declaration".


Associates an exception with a sequence of statements, which is executed when that exception is raised. For the syntax of exception_handler, see "Exception Definition".


A combination of variables, constants, literals, operators, and function calls. The simplest expression consists of a single variable. When the declaration is elaborated, the value of expression is assigned to the parameter. The value and the parameter must have compatible datatypes.


Declares a function. For the syntax of function_declaration, see "Function Declaration".


Parameter modes that define the behavior of formal parameters. An IN parameter passes values to the subprogram being called. An OUT parameter returns values to the caller of the subprogram. An IN OUT parameter lets passes initial values to the subprogram being called and returns updated values to the caller.


Declares a program object. For the syntax of item_declaration, see "Block Declaration".


A compiler hint (not directive) that allows the PL/SQL compiler to pass OUT and IN OUT parameters by reference instead of by value (the default). For more information, see "Specifying Subprogram Parameter Modes".


A formal parameter, which is a variable declared in a procedure spec and referenced in the procedure body.


Marks a function as autonomous. An autonomous transaction is an independent transaction started by the main transaction. Autonomous transactions let you suspend the main transaction, do SQL operations, commit or roll back those operations, then resume the main transaction. For more information, see "Doing Independent Units of Work with Autonomous Transactions".


A user-defined procedure.


Specifies a user-defined datatype. For the syntax of type_definition, see "Block Declaration".


Initializes IN parameters to default values, if they are not specified when the procedure is called.

Usage Notes

A procedure is called as a PL/SQL statement. For example, the procedure raise_salary might be called as follows:

raise_salary(emp_num, amount);

Inside a procedure, an IN parameter acts like a constant; you cannot assign it a value. An OUT parameter acts like a local variable; you can change its value and reference the value in any way. An IN OUT parameter acts like an initialized variable; you can assign it a value, which can be assigned to another variable. For summary information about the parameter modes, see Table 8-1.

Unlike OUT and IN OUT parameters, IN parameters can be initialized to default values. For more information, see "Using Default Values for Subprogram Parameters".

Before exiting a procedure, explicitly assign values to all OUT formal parameters. An OUT actual parameter can have a value before the subprogram is called. However, when you call the subprogram, the value is lost unless you specify the compiler hint NOCOPY or the subprogram exits with an unhandled exception.

You can write the procedure spec and body as a unit. Or, you can separate the procedure spec from its body. That way, you can hide implementation details by placing the procedure in a package. You can define procedures in a package body without declaring their specs in the package spec. However, such procedures can be called only from inside the package. At least one statement must appear in the executable part of a procedure. The NULL statement meets this requirement.


For examples, see the following:

Example 1-12, "Creating a Stored Subprogram"
Example 1-13, "Creating a Package and Package Body"
Example 1-14, "Calling a Procedure in a Package"
Example 9-3, "Creating the emp_admin Package"

Related Topics

"Collection Methods"
"Function Declaration"
"Package Declaration"