PL/SQL Packages
Packages
•Packages are schema objects that groups
logically related PL/SQL types, variables, and
subprograms.
•A package will have two mandatory parts −
•Package specification
•Package body or definition
PL/SQL PACKAGES
Package Specification
• The specification is the interface to the package. It just DECLARES the
types, variables, constants, exceptions, cursors, and subprograms that
can b
• 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. e referenced from outside the package.
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.
Referring Package Elements
• All the public elements of the package can be referred by calling the
package name followed by the element name separated by period i.e.
'<package_name>.<element_name>'.
For Example: package specification
CREATE PACKAGE emp_bonus
AS
PROCEDURE calc_bonus (date_hired employees.hire_date%TYPE);
END emp_bonus;
/
Package Body
CREATE PACKAGE BODY emp_bonus
AS
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;
/
PL/SQL block
SQL> Declare
hire_dt employees.hire_date%TYPE:=&hire_dt;
Begin
emp_bonus.calc_bonus(hire_dt);
End;
/
Example 2:
CREATE PACKAGE cust_sal AS
PROCEDURE find_sal(c_id customers.id%type);
END cust_sal;
/ CREATE OR REPLACE PACKAGE BODY cust_sal AS
PROCEDURE find_sal(c_id customers.id%TYPE) IS
c_sal customers.salary%TYPE;
BEGIN
SELECT salary INTO c_sal
FROM customers
WHERE id = c_id;
dbms_output.put_line('Salary: '|| c_sal);
END find_sal;
END cust_sal;
/
Calling in PLSQL Block
DECLARE
code customers.id%type := &cc_id;
BEGIN
cust_sal.find_sal(code);
END;
/
OUTPUT:
Enter value for cc_id: 1
Salary: 3000
PL/SQL procedure successfully completed.
For Example 3
CREATE OR REPLACE PACKAGE personnel AS
-- get employee's fullname
FUNCTION get_fullname(n_emp_id NUMBER)
RETURN VARCHAR2;
-- get employee's salary
FUNCTION get_salary(n_emp_id NUMBER)
RETURN NUMBER;
END personnel;
CREATE OR REPLACE PACKAGE BODY personnel AS
-- get employee's fullname
FUNCTION get_fullname(n_emp_id NUMBER) RETURN VARCHAR2 IS
v_fullname VARCHAR2(46);
BEGIN
SELECT first_name || ',' || last_name
INTO v_fullname
FROM employees
WHERE employee_id = n_emp_id;
RETURN v_fullname;
EXCEPTION
WHEN NO_DATA_FOUND THEN
RETURN NULL;
WHEN TOO_MANY_ROWS THEN
RETURN NULL;
END;
-- get salary
FUNCTION get_salary(n_emp_id NUMBER) RETURN NUMBER IS
n_salary NUMBER(8,2);
BEGIN
SELECT salary
INTO n_salary
FROM employees
WHERE employee_id = n_emp_id;
RETURN n_salary;
EXCEPTION
WHEN NO_DATA_FOUND THEN
RETURN NULL;
WHEN TOO_MANY_ROWS THEN
RETURN NULL;
END;
END personnel;
SET SERVEROUTPUT
DECLARE
n_salary NUMBER(8,2);
v_name VARCHAR2(46);
n_emp_id NUMBER := &emp_id;
BEGIN
v_name := personnel.get_fullname(n_emp_id);
n_salary := personnel.get_salary(n_emp_id);
IF v_name IS NOT NULL AND n_salary IS NOT NULL
THEN
dbms_output.put_line('Employee: ' || v_name);
dbms_output.put_line('Salary:' || n_salary);
END IF;
END;
CREATE OR REPLACE PACKAGE c_package
AS
PROCEDURE addCustomer(c_id customers.id%type,
c_name customerS.Name%type,
c_age customers.age%type,
c_addr customers.address%type,
c_sal customers.salary%type);
PROCEDURE delCustomer(c_id customers.id%TYPE);
PROCEDURE listCustomer;
END c_package; /
CREATE OR REPLACE PACKAGE BODY c_package
AS
PROCEDURE addCustomer(c_id customers.id%type,
c_name customer.name%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)
VALUES(c_id, c_name, c_age, c_addr, c_sal);
END addCustomer;
PROCEDURE delCustomer(c_id customers.id%type)
IS
BEGIN DELETE FROM customers WHERE id = c_id;
END delCustomer;
PROCEDURE listCustomer
IS
CURSOR c_customers is SELECT name FROM customers;
TYPE c_list is TABLE OF customerS.No.ame%type;
name_list c_list := c_list();
counter integer :=0;
BEGIN
FOR n IN c_customers
LOOP
counter := counter +1;
name_list.extend; name_list(counter) := n.name;
dbms_output.put_line('Customer(' ||counter|| ')'||name_list(counter));
END LOOP;
END listCustomer;
END c_package;
/
PL/SQL Block
DECLARE
code customers.id%type:= 8;
BEGIN
c_package.addcustomer(7, 'Rajnish', 25, 'Chennai', 3500);
c_package.addcustomer(8, 'Subham', 32, 'Delhi', 7500);
c_package.listcustomer;
c_package.delcustomer(code);
c_package.listcustomer;
END;
/

Packages - PL/SQL

  • 1.
  • 2.
    Packages •Packages are schemaobjects that groups logically related PL/SQL types, variables, and subprograms. •A package will have two mandatory parts − •Package specification •Package body or definition
  • 3.
  • 4.
    Package Specification • Thespecification is the interface to the package. It just DECLARES the types, variables, constants, exceptions, cursors, and subprograms that can b • 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. e referenced from outside the package.
  • 5.
    Package Body • Thepackage 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.
  • 6.
    Referring Package Elements •All the public elements of the package can be referred by calling the package name followed by the element name separated by period i.e. '<package_name>.<element_name>'.
  • 7.
    For Example: packagespecification CREATE PACKAGE emp_bonus AS PROCEDURE calc_bonus (date_hired employees.hire_date%TYPE); END emp_bonus; /
  • 8.
    Package Body CREATE PACKAGEBODY emp_bonus AS 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; /
  • 9.
    PL/SQL block SQL> Declare hire_dtemployees.hire_date%TYPE:=&hire_dt; Begin emp_bonus.calc_bonus(hire_dt); End; /
  • 10.
    Example 2: CREATE PACKAGEcust_sal AS PROCEDURE find_sal(c_id customers.id%type); END cust_sal; / CREATE OR REPLACE PACKAGE BODY cust_sal AS PROCEDURE find_sal(c_id customers.id%TYPE) IS c_sal customers.salary%TYPE; BEGIN SELECT salary INTO c_sal FROM customers WHERE id = c_id; dbms_output.put_line('Salary: '|| c_sal); END find_sal; END cust_sal; /
  • 11.
    Calling in PLSQLBlock DECLARE code customers.id%type := &cc_id; BEGIN cust_sal.find_sal(code); END; / OUTPUT: Enter value for cc_id: 1 Salary: 3000 PL/SQL procedure successfully completed.
  • 12.
    For Example 3 CREATEOR REPLACE PACKAGE personnel AS -- get employee's fullname FUNCTION get_fullname(n_emp_id NUMBER) RETURN VARCHAR2; -- get employee's salary FUNCTION get_salary(n_emp_id NUMBER) RETURN NUMBER; END personnel;
  • 13.
    CREATE OR REPLACEPACKAGE BODY personnel AS -- get employee's fullname FUNCTION get_fullname(n_emp_id NUMBER) RETURN VARCHAR2 IS v_fullname VARCHAR2(46); BEGIN SELECT first_name || ',' || last_name INTO v_fullname FROM employees WHERE employee_id = n_emp_id; RETURN v_fullname; EXCEPTION WHEN NO_DATA_FOUND THEN RETURN NULL; WHEN TOO_MANY_ROWS THEN RETURN NULL; END;
  • 14.
    -- get salary FUNCTIONget_salary(n_emp_id NUMBER) RETURN NUMBER IS n_salary NUMBER(8,2); BEGIN SELECT salary INTO n_salary FROM employees WHERE employee_id = n_emp_id; RETURN n_salary; EXCEPTION WHEN NO_DATA_FOUND THEN RETURN NULL; WHEN TOO_MANY_ROWS THEN RETURN NULL; END; END personnel;
  • 15.
    SET SERVEROUTPUT DECLARE n_salary NUMBER(8,2); v_nameVARCHAR2(46); n_emp_id NUMBER := &emp_id; BEGIN v_name := personnel.get_fullname(n_emp_id); n_salary := personnel.get_salary(n_emp_id); IF v_name IS NOT NULL AND n_salary IS NOT NULL THEN dbms_output.put_line('Employee: ' || v_name); dbms_output.put_line('Salary:' || n_salary); END IF; END;
  • 16.
    CREATE OR REPLACEPACKAGE c_package AS PROCEDURE addCustomer(c_id customers.id%type, c_name customerS.Name%type, c_age customers.age%type, c_addr customers.address%type, c_sal customers.salary%type); PROCEDURE delCustomer(c_id customers.id%TYPE); PROCEDURE listCustomer; END c_package; /
  • 17.
    CREATE OR REPLACEPACKAGE BODY c_package AS PROCEDURE addCustomer(c_id customers.id%type, c_name customer.name%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) VALUES(c_id, c_name, c_age, c_addr, c_sal); END addCustomer; PROCEDURE delCustomer(c_id customers.id%type) IS BEGIN DELETE FROM customers WHERE id = c_id; END delCustomer;
  • 18.
    PROCEDURE listCustomer IS CURSOR c_customersis SELECT name FROM customers; TYPE c_list is TABLE OF customerS.No.ame%type; name_list c_list := c_list(); counter integer :=0; BEGIN FOR n IN c_customers LOOP counter := counter +1; name_list.extend; name_list(counter) := n.name; dbms_output.put_line('Customer(' ||counter|| ')'||name_list(counter)); END LOOP; END listCustomer; END c_package; /
  • 19.
    PL/SQL Block DECLARE code customers.id%type:=8; BEGIN c_package.addcustomer(7, 'Rajnish', 25, 'Chennai', 3500); c_package.addcustomer(8, 'Subham', 32, 'Delhi', 7500); c_package.listcustomer; c_package.delcustomer(code); c_package.listcustomer; END; /