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

8

Using PL/SQL Subprograms

Civilization advances by extending the number of important operations that we can perform without thinking about them. —Alfred North Whitehead

This chapter shows you how to turn sets of statements into reusable subprograms. Subprograms are like building blocks for modular, maintainable applications.

This chapter contains these topics:

What Are Subprograms? on page 8-1

Advantages of PL/SQL Subprograms on page 8-2

Understanding PL/SQL Procedures on page 8-3

Understanding PL/SQL Functions on page 8-3

Declaring Nested PL/SQL Subprograms on page 8-5

Passing Parameters to PL/SQL Subprograms on page 8-6

Overloading Subprogram Names on page 8-9

How Subprogram Calls Are Resolved on page 8-12

Using Invoker's Rights Versus Definer's Rights (AUTHID Clause) on page 8-15

Using Recursion with PL/SQL on page 8-20

Calling External Subprograms on page 8-21

Creating Dynamic Web Pages with PL/SQL Server Pages on page 8-22

What Are 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. Generally, you use a procedure to perform an action and a function to compute a value.

Like anonymous blocks, subprograms have:

A declarative part, with declarations of types, cursors, constants, variables, exceptions, and nested subprograms. These items are local and cease to exist when the subprogram ends.

An executable part, with statements that assign values, control execution, and manipulate Oracle data.

An optional exception-handling part, which deals with runtime error conditions.

Using PL/SQL Subprograms 8-1

Advantages of PL/SQL Subprograms

Example 8–1 Simple PL/SQL Procedure

The following example shows a string-manipulation procedure that accepts both input and output parameters, and handles potential errors:

CREATE OR REPLACE PROCEDURE double

(

original IN VARCHAR2, new_string OUT VARCHAR2

)

AS BEGIN

new_string := original || original; EXCEPTION

WHEN VALUE_ERROR THEN

dbms_output.put_line('Output buffer not long enough.');

END;

/

Example 8–2 Simple PL/SQL Function

The following example shows a numeric function that declares a local variable to hold temporary results, and returns a value when finished:

CREATE OR REPLACE FUNCTION square(original NUMBER) RETURN NUMBER

AS

original_squared NUMBER; BEGIN

original_squared := original * original; RETURN original_squared;

END;

/

Advantages of PL/SQL Subprograms

Subprograms let you extend the PL/SQL language. Procedures act like new statements. Functions act like new expressions and operators.

Subprograms let you break a program down into manageable, well-defined modules. You can use top-down design and the stepwise refinement approach to problem solving.

Subprograms promote reusability. Once tested, a subprogram can be reused in any number of applications. You can call PL/SQL subprograms from many different environments, so that you do not have to reinvent the wheel each time you use a new language or API to access the database.

Subprograms promote maintainability. You can change the internals of a subprogram without changing other subprograms that call it. Subprograms play a big part in other maintainability features, such as packages and object types.

Dummy subprograms (stubs) let you defer the definition of procedures and functions until after testing the main program. You can design applications from the top down, thinking abstractly, without worrying about implementation details.

When you use PL/SQL subprograms to define an API, you can make your code even more reusable and maintainable by grouping the subprograms into a PL/SQL package. For more information about packages, see Chapter 9, "Using PL/SQL Packages".

8-2 PL/SQL User's Guide and Reference

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

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

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