Package in Database Management System(DBMS).
Packages:
Packages are defined as database objects use to bundle various sub programs together. It is act as Container which may consists of various datatype, various Variable, Cursor, Procedure , Function etc.
It is created in two separate steps given as follows;
1. Package Specification
2.Package Body
Package Specification:
Package specification acts as an interface to the specification body. It consists of various types of declaration, such as variable, cursor, procedure , function without any coding details.
Anything specify inside package specification by default it taken as a public member which means the number can be used directly from outside of the package.
Any coding done in package body but not specification in package specification are known as private member which means such coding can be used only by the member of the package.
Syntax of package specification:
CREATE PACKAGE package_name
IS|AS
DECLARE
.......BLOCK...............
BEGIN
.......BLOCK...............
EXCEPTION
.......BLOCK...............
END package_name;
Example:
CREATE PACKAGE pack1
IS|AS
PROCEDURE totalamt (e_id.salesrep.emp_id %TYPE);
FUNCTION findemp (cust_id customer.cust_id%TYPE)
RETURN salesrep.emp_id%TYPE;
END pack1;
Package Body:
Package body is actual implementation of package. Inside package body, we implement all the procedures and functions specified inside package specification.
Its syntax is given as follows:
CREATE PACKAGEBODY package_body_name
IS|AS
PROCEDURE procedure_name
IS|AS
DECLARE
...........BLOCK.............
BEGIN
...........BLOCK.............
EXCEPTION
...........BLOCK.............
END;
FUNCTION function_name
(variable DATATYPE,.....) RETURN
IS|AS
DECLARE
...........BLOCK..........
BEGIN
...........BLOCK..........
EXCEPTION
...........BLOCK..........
END;
END package_body;
To execute the member of the package :
package_name.memer_name;
CREATE PACKAGEBODY pack1
IS|AS
PROCEDURE total_amt
(e_id salesrep.emp_id%TYPE)
IS|AS
amt orders.amount%TYPE;
BEGIN
SELECT SUM(amount)
INTO amt
FROM orders,salesrep
WHERE rep=emp_id;
DBMS_OUTPUT.PUT_LINE('total amount is',||amt);
END;
FUNCTION findemp (cust_id suctomer.custid %TYPE)
IS|AS
BEGIN
SELECT custrep INTO eid
FROM customer
WHERE custid=cust_id;
RETURN eid;
END;
DECLARE
c_id customer.sudt_id%TYPE:=&cust_id;
e_id salesrep.emp_id%TYPE;
BEGIN
e_id:=pack1.findemp(c_id);
pack1.totalamt(c_id);
END;
Comments
Post a Comment