Beruflich Dokumente
Kultur Dokumente
Packages - Objectives
At the end of this session, the participants will be able to:
Understand the need for Packages Write a Package specification Write a Package body Overloading subprograms
Packages - Contents
Package Specification Package Body Forward declarations Overloading Subprograms
Packages
A Package is a PL/SQL construct that allows related objects to be stored together A Package is a stored database object Groups together procedures, functions, cursor, types and variables Other PL/SQL blocks can reference packages
Packages (contd)
Package has two components: Package Specification Package body Package Specification: Declares types, variables, constants, exceptions, cursors and subprograms available for use. Constructs declared in the package specifications are public. Syntax: CREATE OR REPLACE PACKAGE package_name IS procedure_specification function_specification variable_declaration cursor_declaration etc. END package_name;
Packages (contd)
Package Body:
It defines cursors and subprograms and so implements the specification It can not be successfully compiled unless the package header has already been successfully compiled
Syntax:
CREATE OR REPLACE PACKAGE BODY package_name IS private_variable procedure_definition function_definition etc. private function or procedure definition END package_name;
Packages
A package body contains the blocks and specifications for all of the public objects listed in the package specification. The package body may include objects that are not listed in the package specification. Such object are said to be private and are not available to users of the package. Private objects may only be called by other objects within the same package body.
Packages - Advantages
Advantages of Packages
1. Modularity: encapsulates logically related programming structures in a named module 2. Easier Application Design: Initially only the interface information in the package specification is required 3. Information hiding: Constructs can be public or private 4. Better Performance: When a packaged subprogram is called the first time, the entire package is loaded into memory 5. Overloading: Packages allow you to overload procedures and functions
Packages: Example
Creating a Package Specification
CREATE OR REPLACE PACKAGE emp_pack IS PROCEDURE emp_add(p_empno IN emp.empno%TYPE, p_ename IN emp.ename%TYPE,p_esal IN emp.sal%TYPE, p_deptno IN emp.deptno%TYPE); PROCEDURE emp_del (p_empno IN emp.empno%TYPE); END emp_pack;
Packages: Overloading
Allows you to use the same name for different subprograms inside a package Requires formal parameters of the subprogram to differ in number, order or data type family
REPLACE PACKAGE PACK_MARKETING AS MKT_PROJECTION; MKT_PROJECTION(MK_ID NUMBER); MKT_PROJECTION (MK_NAME VARCHAR2); MKT_PROJECTION(MK_ID NUMBER,MK_NAME VARCHAR2); PROCEDURE MKT_PROJECTION(MK_NAME VARCHAR2, MK_ID NUMBER); END PACK_MARKETING;
Packages
When creating packages, the package specification and the package body are created separately. Thus, there are two commands to use: create package for the package specification, and create package body for the package body. Both of these commands require that you have the CREATE PROCEDURE system privilege. If the package is to be created in a schema other than your own, then you must have the CREATE ANY PROCEDURE system privilege.
Dropping Package
Dropping package specification: DROP PACKAGE package_name; Dropping package body: DROP PACKAGE BODY package_name;
Exercise
CREATE OR REPLACE PACKAGE BB_PACK IS V_MAX_TEAM_SALARY NUMBER(12,2); PROCEDURE ADD_PLAYER(V_ID IN NUMBER, V_LAST_NAME VARCHAR2, V_SALARY NUMBER);
END BB_PACK;
Exercise (Cont..)
CREATE OR REPLACE PACKAGE BODY BB_PACK IS PROCEDURE UPD_PLAYER_STAT (V_ID IN NUMBER, V_AB IN NUMBER DEFAULT 4, V_HITS IN NUMBER) IS BEGIN UPDATE PLAYER_BAT_STAT SET AT_BATS = AT_BATS + V_AB, HITS = HITS + V_HITS WHERE PLAYER_ID = V_ID; COMMIT; END UPD_PLAYER_STAT;
Exercise (Cont..)
PROCEDURE ADD_PLAYER (V_ID IN NUMBER, V_LAST_NAME VARCHAR2, V_SALARY NUMBER) IS BEGIN INSERT INTO PLAYER(ID,LAST_NAME,SALARY) VALUES (V_ID, V_LAST_NAME, V_SALARY); UPD_PLAYER_STAT(V_ID,0,0); END ADD_PLAYER; END BB_PACK;
Exercise (Cont..)
You make a change to the body of the BB_PACK package. The BB_PACK body is recompiled. What happens if the stand alone procedure VALIDATE_PLAYER_STAT references this package? A. VALIDATE_PLAYER_STAT cannot recompile and must be recreated. B. VALIDATE_PLAYER_STAT is not invalidated. C. VALDIATE_PLAYER_STAT is invalidated. D. VALIDATE_PLAYER_STAT and BB_PACK are invalidated.
Summary
We have discussed:
Parts of a Package Coding a procedure/function within a Package Overloading Subprograms within a Package
Questions
Thank You