Está en la página 1de 3

11/5/2018 Packages | DBMS | Tutorialink.

com

Packages
"A package is a container for other database objects."
A package can hold other database objects such as variables , consatants,cursors,excep ons,procedures,func ons and sub-
programs.
A package has usually two components, a specifica on and a body.
A package's specifica on declares the types (variables of the record type), memory variables, constants, excep ons, cursors,
and subprograms that are available for use.
A package's body fully defines cursors, func ons, and procedures and thus implements the specifica on.

Package specifica on:


The package specifica on contains:
Name of the package
Names of the data types of any arguments
This declara on is local to the database and global to the package
This means that procedures, func ons, variables, constants, cursors and excep ons and other objects, declared in a package
are accessible from anywhere in the package.
Therefore, all the informa on a package needs, to execute a stored subprogram, is contained in the package specifica ons
itself.

Syntax:
Create [or replace] package package_name
{is | as}
Package_specification
End package_name;

Example:
create package opera on that contains procedure 'add' and func on 'sub'.

CREATE OR REPLACE PACKAGE OPERATION


IS
PROCEDURE ADDITION(A IN NUMBER,B IN NUMBER);
FUNCTION SUB(A IN NUMBER,B IN NUMBER)RETURN NUMBER;
END OPERATION;
/

Output:
Run SQL Command Line

SQL>start D://pac.sql
Package created.

https://tutorialink.com/dbms/packages.dbms 1/3
11/5/2018 Packages | DBMS | Tutorialink.com

Package Body :
The body of a package contains the defini on of public objects that are declared in the specifica on.
The body can also contain other object declara ons that are private to the package.
The objects declared privately in the package body are not accessible to other objects outside the package.
Unlike package specifica on, the package body can contain subprogram bodies.
A er the package is wri en, debugged, compiled and stored in the database applica ons can reference the package's types,
call its subprograms, use its cursors, or raise its excep ons.

Syntax:
Create [or replace] package body package_name
{is | as}
Package_body
End package_name;

Example:
Create or replace package body OPERATION
is
PROCEDURE ADDITION(A IN NUMBER,B IN NUMBER)
is
begin
dbms_output.put_line('addtion of two number :'||ADD(A,B));
end;
FUNCTION SUB(A IN NUMBER,B IN NUMBER)RETURN NUMBER
is
ans number(3);
begin
ans:=A-B;
return ans;
end;
end OPERATION;
/

Output:
Run SQL Command Line

SQL>start D://pacbody.sql
Package body created.

Advantages of package:
Packages enable the organiza on of commercial applica ons into efficient modules. Each package is easily understood and
the interfaces between packages are simple, clear and well defined.
Packages allow gran ng of privileges efficiently .
https://tutorialink.com/dbms/packages.dbms 2/3
11/5/2018 Packages | DBMS | Tutorialink.com

A package's public variables and cursors persist for the dura on of the session. Therefore all cursors and procedures that
execute in this environment can share them .
Packages enable the overloading of procedures and func ons when required .
Packages improve performance by loading mul ple objects into memory at once. Therefore, subsequent calls to related
subprograms in the package require no i/o .
Packages promote code reuse through the use of libraries that contain stored procedures and func ons, thereby reducing
redundant coding .

https://tutorialink.com/dbms/packages.dbms 3/3

También podría gustarte