Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Скачиваний:
37
Добавлен:
16.04.2013
Размер:
5.97 Mб
Скачать

Procedures

Procedures

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" on

page 8-3.

Syntax

procedure_spec

 

 

 

,

 

 

 

 

 

 

 

 

 

 

 

(

parameter_declaration

)

PROCEDURE

procedure_name

 

 

 

;

procedure_declaration | procedure body

 

 

 

 

 

 

OR REPLACE

 

 

 

 

CREATE

 

 

 

 

 

 

 

 

 

 

PROCEDURE

procedure_name

 

 

,

 

 

CURRENT_USER

 

 

 

 

AUTHID

 

 

(

parameter_declaration

)

 

DEFINER

IS

AS

13-104 PL/SQL User's Guide and Reference

Procedures

PRAGMA AUTONOMOUS_TRANSACTION

;

 

 

type_definition

 

function_declaration

 

 

item_declaration

procedure_declaration

 

 

 

 

 

BEGIN

statement

EXCEPTION

exception_handler

procedure_name

 

 

 

 

END

;

 

parameter_declaration

IN

 

 

 

 

 

 

 

 

OUT

NOCOPY

 

 

 

IN OUT

 

 

 

parameter_name

 

datatype

 

 

:=

expression

DEFAULT

Keyword and Parameter Description

AUTHID

Determines whether a stored procedure executes with the privileges of its owner (the default) or current user and whether its unqualified references to schema objects are resolved in the schema of the owner or current user. You can override the default behavior by specifying CURRENT_USER. For more information, see "Using Invoker's Rights Versus Definer's Rights (AUTHID Clause)" on page 8-15.

CREATE

The optional CREATE clause creates stored procedures, which are stored in the Oracle database and can be called from other applications. You can execute the CREATE statement interactively from SQL*Plus or from a program using native dynamic SQL.

datatype

A type specifier. For the syntax of datatype, see "Constants and Variables" on page 13-28.

exception_handler

Associates an exception with a sequence of statements, which is executed when that exception is raised. For the syntax of exception_handler, see "Exceptions" on page 13-45.

expression

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.

PL/SQL Language Elements 13-105

Procedures

function_declaration

Declares a function. For the syntax of function_declaration, see "Functions" on page 13-67.

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 IN OUT parameter lets passes initial values to the subprogram being called and returns updated values to the caller.

item_declaration

Declares a program object. For the syntax of item_declaration, see "Blocks" on page 13-8.

NOCOPY

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" on page 8-7.

parameter_name

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

PRAGMA AUTONOMOUS_TRANSACTION

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" on page 6-35.

procedure_name

A user-defined procedure.

type_definition

Specifies a user-defined datatype. For the syntax of type_definition, see "Blocks" on page 13-8.

:= | DEFAULT

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 on page 8-8.

13-106 PL/SQL User's Guide and Reference

Procedures

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

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.

Examples

The following procedure debits a bank account:

PROCEDURE debit_account (acct_id INTEGER, amount REAL) IS old_balance REAL;

new_balance REAL; overdrawn EXCEPTION;

BEGIN

SELECT bal INTO old_balance FROM accts WHERE acctno = acct_id; new_balance := old_balance - amount;

IF new_balance < 0 THEN RAISE overdrawn;

ELSE

UPDATE accts SET bal = new_balance WHERE acctno = acct_id; END IF;

EXCEPTION

WHEN overdrawn THEN

...

END debit_account;

The following example calls the procedure using named notation:

debit_account(amount => 500, acct_id => 10261);

Related Topics

Collection Methods, Functions, Packages

PL/SQL Language Elements 13-107

Тут вы можете оставить комментарий к выбранному абзацу или сообщить об ошибке.

Оставленные комментарии видны всем.

Соседние файлы в папке Oracle 10g