0% found this document useful (0 votes)
76 views

RDBMS (Packages)

Packages in PL/SQL allow grouping of related objects and provide modularity. A package has two parts - a specification and an optional body. The specification declares objects publicly while the body defines and implements them. Specifications ensure interface compatibility while bodies can be changed without affecting other code. Packages improve code organization, error checking and security of the database.

Uploaded by

Pruthvi Selukar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
76 views

RDBMS (Packages)

Packages in PL/SQL allow grouping of related objects and provide modularity. A package has two parts - a specification and an optional body. The specification declares objects publicly while the body defines and implements them. Specifications ensure interface compatibility while bodies can be changed without affecting other code. Packages improve code organization, error checking and security of the database.

Uploaded by

Pruthvi Selukar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

PACKAGES ( by- Somnath Dhonde)

 A package is a schema object that groups logically related PL/SQL


types, variables, and subprograms.

 Packages usually have two parts, a specification (spec) and a


body; sometimes the body is unnecessary.

 The specification is the interface to the package. It declares the


types, variables, constants, exceptions, cursors, and subprograms
that can be referenced from outside the package.

 The body defines the queries for the cursors and the code for the
subprograms.

 DEFINITION :

Packages are stored libraries in the database which allow us to


group related PL/SQL objects under one name. Or in simple
words, Packages are logical groups of related PL/SQL objects.

 PACKAGE ARCHITECTURE :

 PL/SQL package is divided into two parts:

1. The Package Specification, also known as the Header


2. The Package Body

 Both these parts are stored separately in the data dictionary. The
package specification is the required part whereas the package
body is optional, but it is a good practice to provide the body to the
package.
PACKAGE SPECIFICATION:
 Package specification is also known as the package header.

 It is the section where we put the declaration of all the package


elements.

 Whatever elements we declare here in this section are publicly


available and can be referenced outside of the package.

 In this section we only declare package elements but we don’t


define them. Also this is the mandatory section of the package.

 Syntax of Package specifications:

CREATE OR REPALCE PACKAGE pkg_name IS

Declaration of all the package element…;

END [pkg_name];

 Example 1:

 A Simple Package Specification Without a Body


CREATE PACKAGE trans_data AS -- bodiless package
TYPE TimeRec IS RECORD (
minutes SMALLINT,
hours SMALLINT);
TYPE TransRec IS RECORD (
category VARCHAR2(10),
account INT,
amount REAL,
time_of TimeRec);
minimum_balance CONSTANT REAL := 10.00;
number_processed INT;
insufficient_funds EXCEPTION;
END trans_data;
/

PACKAGE BODY: ( by – Pawan Gutthe)


 If a package specification declares cursors or subprograms, then a
package body is required; otherwise, it is optional .

 In package body we provide the actual structure to all the package


elements which we have already declared in the specification by
programming them.

 we can say that a package body contains the implementation of


the elements listed in the package specification.

 Syntax of the package body:-

CREATE OR REPALCE PACKAGE BODY pkg_name IS


Variable declaration;
Type Declaration;
BEGIN
Implementation of the package elements…
END [pkg_name];
 Example 2 :

- Matching Package Specifications and Bodies

CREATE PACKAGE emp_bonus AS


PROCEDURE calc_bonus (date_hired employees.hire_date%TYPE);

END emp_bonus;
/
CREATE PACKAGE BODY emp_bonus AS

-- the following parameter declaration raises an exception


-- because 'DATE' does not match employees.hire_date%TYPE
-- PROCEDURE calc_bonus (date_hired DATE) IS

-- the following is correct because there is an exact match


PROCEDURE calc_bonus (date_hired employees.hire_date%TYPE) IS
BEGIN

DBMS_OUTPUT.PUT_LINE('Employees hired on ' || date_hired || ' get


bonus.');

END;
END emp_bonus;

Reference link.
 https://docs.oracle.com/cd/B19306_01/appdev.102/b14261/packages.htm
 http://www.rebellionrider.com/introduction-to-pl-sql-packages-in-oracle-
database/#.WIYwa_l97IU

You might also like