A Package is a schema object that groups logically related PL/SQL types, items and subprograms like procedures and functions. Package can hold other constructs too, such as exceptions, variables, cursors and type declarations.
Package have two parts:
- Package specification
- Package body
- Modularity: packages let us encapsulate logically related types, items and subprogrmas in a named PL/SQL module.
- Easier application design : when designing an application, initially need to mention only interface information in the package specs, we can define the detailed body code later.
- Information hiding : with packages, we can specify which types , items and subprograms are public or private.
- Better pefformance: when we call a packaged subprogram for the first time, the whole package is loaded into the memory. So, later calls to related subprograms in the package require no disk I/O.
- Added functionality: packaged public variables and cursors persist for the duration of a session so, they cab be shared by all the subprograms that execute in the environment.
PROCEDURE ADD_CUSTOMER
(
c_id IN NUMBER,
c_fname IN VARCHAR2,
c_lname IN VARCHAR2,
c_addr IN VARCHAR2,
c_date_added IN DATE
);
PROCEDURE DISPLAY_NAMES;
PROCEDURE GET_CUSTOMER
(
c_id IN NUMBER
);
PROCEDURE show_customer
(
customer_in IN customer%ROWTYPE
);
FUNCTION get_names
(
custid IN NUMBER
)
RETURN SYS_REFCURSOR;
FUNCTION find_salescount
(
p_sales_date IN date
)
RETURN NUMBER;
END CUSTOMER_PACKAGE;
PROCEDURE ADD_CUSTOMER
(
c_id IN OUT NUMBER,
c_fname IN VARCHAR2,
c_lname IN VARCHAR2,
c_addr IN VARCHAR2,
c_date_added IN DATE
)
AS
BEGIN
INSERT INTO CUSTOMER(customer_id, first_name, last_name, address, date_added) VALUES (c_id,c_fname,c_lname,c_address,c_date_added);
COMMIT;
dbms_output.put_line('Data successfully Inserted');
SELECT COUNT(1) INTO c_id FROM CUSTOMER;
END ADD_CUSTOMER;
PROCEDURE DISPLAY_NAMES
IS
C_REC SYS_REFCURSOR;
FNAME VARCHAR2(50);
LNAME VARCHAR2(50);
BEGIN
C_REC := GET_NAMES(10);
LOOP
FETCH C_REC INTO FNAME , LANME;
EXIT WHEN C_REC%NOTFOUND;
DBMS_OUTPUT.PUT_LINE(FNAME);
DBMS_OUTPUT.PUT_LINE(LNAME);
END LOOP;
CLOSE C_REC;
END;
FUNCTION find_salescount
( p_sales_date IN date
) RETURN NUMBER
AS
no_of_sales number := 0;
BEGIN
SELECT count(*) INTO no_of_sales from sales where sales_date = p_sales_date;
RETURN no_of_sales;
END find_salescount;
FUNCTION get_names
(
custid IN NUMBER
)
RETURN SYS_REFCURSOR;
IS
l_return SYS_REFCURSOR;
BEGIN
OPEN l_return FOR
SELECT first_name, last_name FROM customer WHERE customer_id = custid;
RETURN l_return;
END get_names;