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

Advantages of PL/SQL Packages

Variables that you want to remain available between procedure calls in the same session. You can treat variables in a package like global variables.

Type declarations for PL/SQL collection types. To pass a collection as a parameter between stored procedures or functions, you must declare the type in a package so that both the calling and called subprogram can refer to it.

Example of a PL/SQL Package

The example below packages a record type, a cursor, and two employment procedures. The procedure hire_employee uses the sequence empno_seq and the function SYSDATE to insert a new employee number and hire date.

CREATE OR REPLACE PACKAGE emp_actions AS -- spec TYPE EmpRecTyp IS RECORD (emp_id INT, salary REAL); CURSOR desc_salary RETURN EmpRecTyp;

PROCEDURE hire_employee ( ename VARCHAR2,

job VARCHAR2, mgr NUMBER, sal NUMBER, comm NUMBER, deptno NUMBER);

PROCEDURE fire_employee (emp_id NUMBER); END emp_actions;


CREATE OR REPLACE PACKAGE BODY emp_actions AS -- body CURSOR desc_salary RETURN EmpRecTyp IS

SELECT empno, sal FROM emp ORDER BY sal DESC; PROCEDURE hire_employee (

ename VARCHAR2, job VARCHAR2, mgr NUMBER, sal NUMBER, comm NUMBER, deptno NUMBER) IS


INSERT INTO emp VALUES (empno_seq.NEXTVAL, ename, job, mgr, SYSDATE, sal, comm, deptno);

END hire_employee;

PROCEDURE fire_employee (emp_id NUMBER) IS BEGIN

DELETE FROM emp WHERE empno = emp_id; END fire_employee;

END emp_actions;


Only the declarations in the package spec are visible and accessible to applications. Implementation details in the package body are hidden and inaccessible. You can change the body (implementation) without having to recompile calling programs.

Advantages of PL/SQL Packages

Packages have a long history in software engineering, offering important features for reliable, maintainable, reusable code, often in team development efforts for large systems.

Using PL/SQL Packages 9-3

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