Welcome to Scribd, the world's digital library. Read, publish, and share books and documents. See more
Download
Standard view
Full view
of .
Look up keyword
Like this
24Activity
0 of .
Results for:
No results containing your search query
P. 1
Packages

Packages

Ratings: (0)|Views: 471 |Likes:
Published by api-3831209

More info:

Published by: api-3831209 on Oct 18, 2008
Copyright:Attribution Non-commercial

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as DOC, PDF, TXT or read online from Scribd
See more
See less

03/18/2014

pdf

text

original

What Is a Package?
-

Apackage is a database object that groups
logically related PL/SQL types, objects, and
subprograms.

-

Packages usually have two parts, a specification
and a body, although sometimes the body is
unnecessary.

-

Thespecification is the interface to your
applications; it declares the types, variables,
constants, exceptions, cursors, and subprograms
available for use.

-
Thebody fully defines cursors and subprograms,
and so implements the specification.
-
Unlike subprograms, packages cannot be called,
parameterized, or nested.
-
Still, the format of a package is similar to that
of a subprogram:

CREATE PACKAGE name AS -- specification (visible part)
-- public type and object declarations
-- subprogram specifications

END [name];

CREATE PACKAGE BODY name AS -- body (hidden part)
-- private type and object declarations
-- subprogram bodies

[BEGIN
-- initialization statements]
END [name];
-
The specification holdspublic declarations, which
are visible to your application.
-

The body holds implementation details andprivate
declarations, which are hidden from your
application.

-

you can think of the specification as an
operational interface and of the body as a "black
box":

Figure 8 -
1. Package Interface
-

You can debug, enhance, or replace a package body
without changing the interface (package
specification) to the package body.

-

To create packages and store them permanently in
an Oracle database, you use the CREATE PACKAGE and
CREATE PACKAGE BODY statements, which you can
execute interactively from SQL*Plus or Server
Manager.

-

In the example below, you package a record type, a
cursor, and two employment procedures. Notice that
the procedurehire_employee uses the database
sequenceempno_seq and the function SYSDATE to
insert a new employee number and hire date,
respectively.

CREATE PACKAGE emp_actions AS -- specification
TYPE EmpRecTyp IS RECORD (emp_id INTEGER, 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 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
BEGIN
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 specification
are visible and accessible to applications.
-
Implementation details in the package body are
hidden and inaccessible.
-
So, you can change the body (implementation)
without having to recompile calling programs.
Advantages of Packages
-

Packages offer several advantages: modularity, easier application design, information hiding, added functionality, and better performance.

Modularity
-

Packages let you encapsulate logically related
types, objects, and subprograms in a named PL/SQL
module.

-

Each package is easy to understand, and the
interfaces between packages are simple, clear, and
well defined.

-
This aids application development.
Easier Application Design
-

When designing an application, all you need
initially is the interface information in the
package specifications.

-
You can code and compile a specification without
its body.
-

Once the specification has been compiled, stored
subprograms that reference the package can be
compiled as well.

-
You need not define the package bodies fully until
you are ready to complete the application.
Information Hiding
-

With packages, you can specify which types,
objects, and subprograms are public (visible and
accessible) or private (hidden and inaccessible).

Activity (24)

You've already reviewed this. Edit your review.
1 hundred reads
1 thousand reads
Amit Kasana liked this
jayavardhankoti liked this
Debasis Mohanty liked this
mann2323 liked this
anilpabathi liked this
Abhishek Pandey liked this
dipeshk liked this
nia1234567890 liked this

You're Reading a Free Preview

Download
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->