Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Programming PL SQL.doc
Скачиваний:
3
Добавлен:
01.07.2025
Размер:
5.06 Mб
Скачать

16.2 Procedures

A procedure is a module that performs one or more actions. Because a procedure call is a standalone executable statement in PL/SQL, a PL/SQL block could consist of nothing more than a single call to a procedure. Procedures are key building blocks of modular code, allowing you to both consolidate and reuse your program logic.

The general format of a PL/SQL procedure is as follows:

PROCEDURE [schema.]name [( parameter [, parameter ...] ) ]

[AUTHID DEFINER | CURRENT_USER]

IS

[declarations]

BEGIN

executable statements

[ EXCEPTION

exception handlers]

END [name];

where each element is used in the following ways:

schema

Optional name of the schema that will own this procedure. The default is the current user. If different from the current user, that user will need privileges to create a procedure in another schema.

name

The name of the procedure, which comes directly after the keyword PROCEDURE.

parameters

An optional list of parameters that you define to both pass information into the procedure, and send information out of the procedure back to the calling program.

AUTHID clause

Determines whether the procedure will execute under the authority of the definer (owner) of the procedure or under the authority of the current user. The former is known as the definer rights model, the latter as the invoker rights model.

declarations

The declarations of local identifiers for that procedure. If you do not have any declarations, there will be no statements between the IS and BEGIN statements.

executable statements

The statements that the procedure executes when it is called. You must have at least one executable statement after the BEGIN and before the END or EXCEPTION keywords.

exception handlers

The optional exception handlers for the procedure. If you do not explicitly handle any exceptions, then you can leave out the EXCEPTION keyword and simply terminate the execution section with the END keyword.

Figure 16-1 shows the apply_discount procedure, which contains all four sections of the named PL/SQL block as well as a parameter list.

Figure 16-1. The apply_discount procedure

16.2.1 Calling a Procedure

A procedure is called as an executable PL/SQL statement. In other words, a call to a procedure must end with a semicolon (;) and be executed before and after other SQL or PL/SQL statements (if they exist) in the execution section of a PL/SQL block.

The following executable statement runs the apply_discount procedure:

BEGIN

apply_discount( new_company_id, 0.15 ); -- 15% discount

END;

If the procedure does not have any parameters, then you call the procedure without any parentheses:

display_store_summary;

In Oracle8i and later, you can also include empty open and close parentheses as well, as in:

display_store_summary( );

16.2.2 The Procedure Header

The portion of the procedure definition that comes before the IS keyword is called the procedure header. The header provides all the information a programmer needs to call that procedure, namely:

  • The procedure name

  • The AUTHID clause, if any

  • The parameter list, if any

A programmer does not need to know about the inside of the procedure to be able to call it properly from another program.

The header for the apply_discount procedure mentioned in the previous section is:

PROCEDURE apply_discount

(company_id_in IN company.company_id%TYPE,

discount_in IN NUMBER)

It consists of the module type, the name, and a list of two parameters.

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]