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

Guidelines for Writing Packages

CREATE OR REPLACE PROCEDURE list_tables AS BEGIN

dbms_output.put_line('These are the tables you own:'); FOR item IN (SELECT table_name FROM user_tables)

LOOP dbms_output.put_line(item.table_name);

END LOOP; END;

/

When you issue the following commands, SQL*Plus displays the output from the procedure:

SQL> SET SERVEROUTPUT ON

SQL> EXEC list_tables;

If the output is long, you might need to issue SET SERVEROUTPUT ON SIZE 1000000 to use a bigger output buffer.

About the DBMS_PIPE Package

Package DBMS_PIPE allows different sessions to communicate over named pipes. (A pipe is an area of memory used by one process to pass information to another.) You can use the procedures pack_message and send_message to pack a message into a pipe, then send it to another session in the same instance or to a waiting application such as a UNIX program.

At the other end of the pipe, you can use the procedures receive_message and unpack_message to receive and unpack (read) the message. Named pipes are useful in many ways. For example, you can write a C program to collect data, then send it through pipes to stored procedures in an Oracle database.

About the UTL_FILE Package

Package UTL_FILE lets PL/SQL programs read and write operating system (OS) text files. It provides a restricted version of standard OS stream file I/O, including open, put, get, and close operations.

When you want to read or write a text file, you call the function fopen, which returns a file handle for use in subsequent procedure calls. For example, the procedure put_ line writes a text string and line terminator to an open file, and the procedure get_ line reads a line of text from an open file into an output buffer.

About the UTL_HTTP Package

Package UTL_HTTP allows your PL/SQL programs to make hypertext transfer protocol (HTTP) callouts. It can retrieve data from the Internet or call Oracle Web Server cartridges. The package has two entry points, each of which accepts a URL (uniform resource locator) string, contacts the specified site, and returns the requested data, which is usually in hypertext markup language (HTML) format.

Guidelines for Writing Packages

When writing packages, keep them general so they can be reused in future applications. Become familiar with the Oracle-supplied packages, and avoid writing packages that duplicate features already provided by Oracle.

Using PL/SQL Packages 9-13

Separating Cursor Specs and Bodies with Packages

Design and define package specs before the package bodies. Place in a spec only those things that must be visible to calling programs. That way, other developers cannot build unsafe dependencies on your implementation details.

To reduce the need for recompiling when code is changed, place as few items as possible in a package spec. Changes to a package body do not require recompiling calling procedures. Changes to a package spec require Oracle to recompile every stored subprogram that references the package.

Separating Cursor Specs and Bodies with Packages

You can separate a cursor specification (spec for short) from its body for placement in a package. That way, you can change the cursor body without having to change the cursor spec. You code the cursor spec in the package spec using this syntax:

CURSOR cursor_name [(parameter[, parameter]...)] RETURN return_type;

In the following example, you use the %ROWTYPE attribute to provide a record type that represents a row in the database table emp:

CREATE PACKAGE emp_stuff AS

CURSOR c1 RETURN emp%ROWTYPE; -- declare cursor spec

...

END emp_stuff;

/

CREATE PACKAGE BODY emp_stuff AS CURSOR c1 RETURN emp%ROWTYPE IS

SELECT * FROM emp WHERE sal > 2500; -- define cursor body

...

END emp_stuff;

/

The cursor spec has no SELECT statement because the RETURN clause specifies the datatype of the return value. However, the cursor body must have a SELECT statement and the same RETURN clause as the cursor spec. Also, the number and datatypes of items in the SELECT list and the RETURN clause must match.

Packaged cursors increase flexibility. For example, you can change the cursor body in the last example, without having to change the cursor spec.

From a PL/SQL block or subprogram, you use dot notation to reference a packaged cursor, as the following example shows:

DECLARE

emp_rec employees%ROWTYPE; BEGIN

OPEN emp_stuff.c1; LOOP

FETCH emp_stuff.c1 INTO emp_rec; /* Do more processing here... */ EXIT WHEN emp_suff.c1%NOTFOUND;

END LOOP;

CLOSE emp_stuff.c1; END;

/

The scope of a packaged cursor is not limited to a PL/SQL block. When you open a packaged cursor, it remains open until you close it or you disconnect from the session.

9-14 PL/SQL User's Guide and Reference

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