Notes Chapter 3.1 Lecture 1.4 (Package)
Notes Chapter 3.1 Lecture 1.4 (Package)
CHAPTER 3.1
● Package specification
Package Specification
The specification is the interface to the package. It just DECLARES the types, variables,
constants, exceptions, cursors, and subprograms that can be referenced from outside the
package. In other words, it contains all information about the content of the package, but
excludes the code for the subprograms.
All objects placed in the specification are called public objects. Any subprogram not in the
package specification but coded in the package body is called a private object.
The following code snippet shows a package specification having a single procedure. You
can have many global variables defined and multiple procedures or functions inside a
package.
END cust_sal;
When the above code is executed at the SQL prompt, it produces the following result −
Package created.
Package Body
The package body has the codes for various methods declared in the package specification
and other private declarations, which are hidden from the code outside the package.
The CREATE PACKAGE BODY Statement is used for creating the package body. The
following code snippet shows the package body declaration for the cust_sal package created
above. I assumed that we already have CUSTOMERS table created in our database as
mentioned in the PL/SQL - Variables chapter.
c_sal customers.salary%TYPE;
BEGIN
FROM customers
WHERE id = c_id;
END find_sal;
END cust_sal;
When the above code is executed at the SQL prompt, it produces the following result −
The package elements (variables, procedures or functions) are accessed with the following
syntax −
package_name.element_name;
Consider, we already have created the above package in our database schema, the following
program uses the find_sal method of the cust_sal package −
DECLARE
BEGIN
cust_sal.find_sal(code);
END;
When the above code is executed at the SQL prompt, it prompts to enter the customer ID and
when you enter an ID, it displays the corresponding salary as follows −
Salary: 3000
Example
● The following program provides a more complete package. We will use the
CUSTOMERS table stored in our database with the following records −
6 Komal 22 MP 5500.00
-- Adds a customer
c_name customerS.No.ame%type,
c_age customers.age%type,
c_addr customers.address%type,
c_sal customers.salary%type);
-- Removes a customer
PROCEDURE listCustomer;
END c_package;
When the above code is executed at the SQL prompt, it creates the above package and
displays the following result −
Package created.
c_name customerS.No.ame%type,
c_age customers.age%type,
c_addr customers.address%type,
c_sal customers.salary%type)
IS
BEGIN
INSERT INTO customers (id,name,age,address,salary)
END addCustomer;
BEGIN
WHERE id = c_id;
END delCustomer;
PROCEDURE listCustomer IS
CURSOR c_customers is
BEGIN
name_list.extend;
name_list(counter) := n.name;
END LOOP;
END listCustomer;
END c_package;
The above example makes use of the nested table. We will discuss the concept of nested table
in the next chapter.
When the above code is executed at the SQL prompt, it produces the following result −
The following program uses the methods declared and defined in the package c_package.
DECLARE
code customers.id%type:= 8;
BEGIN
c_package.listcustomer;
c_package.delcustomer(code);
c_package.listcustomer;
END;
When the above code is executed at the SQL prompt, it produces the following result −
Customer(1): Ramesh
Customer(2): Khilan
Customer(3): kaushik
Customer(4): Chaitali
Customer(5): Hardik
Customer(6): Komal
Customer(7): Rajnish
Customer(8): Subham
Customer(1): Ramesh
Customer(2): Khilan
Customer(3): kaushik
Customer(4): Chaitali
Customer(5): Hardik
Customer(6): Komal
Customer(7): Rajnish
● PLSQL Package helps to hide information with the help of public and private items,
data types, and subprograms.
● package allows maintaining information over all the transactions without requiring to
store it in the database.