|Oracle® Database PL/SQL User's Guide and Reference
10g Release 2 (10.2)
|PDF · Mobi · ePub|
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
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
PROCEDURE SQL statement, see Oracle Database SQL Reference.
procedure specification ::=
procedure declaration ::=
procedure body ::=
parameter declaration ::=
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".
IN, OUT, IN OUT
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
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 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".
:= | DEFAULT
IN parameters to default values, if they are not specified when the procedure is called.
A procedure is called as a PL/SQL statement. For example, the procedure
raise_salary might be called as follows:
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
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.
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: