Sie sind auf Seite 1von 23

Packages

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: Example (contd)


Creating a Package Body CREATE OR REPLACE PACKAGE BODY emp_pack IS FUNCTION valid_deptno(p_deptno dept.deptno%TYPE) RETURN BOOLEAN IS dummy NUMBER; BEGIN SELECT deptno into dummy FROM dept WHERE deptno=p_deptno; RETURN TRUE; EXCEPTION WHEN NO_DATA_FOUND THEN RETURN FALSE; END; Contd

Packages: Example (contd)


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) IS BEGIN IF valid_deptno(p_deptno) THEN INSERT INTO emp(empno,ename,sal,deptno) VALUES(p_empno,p_ename,p_esal,p_deptno); DBMS_OUTPUT.PUT_LINE('Emp# : '||p_empno||' added to table'); COMMIT; ELSE RAISE_APPLICATION_ERROR(-20010,Invalid departmentTry again); END IF END;
Contd

Packages: Example (contd)


PROCEDURE emp_del(p_empno IN emp.empno%TYPE) IS BEGIN DELETE FROM emp WHERE empno=p_empno; IF SQL%NOTFOUND THEN RAISE_APPLICATION_ERROR(-20120,Employee Not Deleted); ELSE DBMS_OUTPUT.PUT_LINE(SQL%ROWCOUNT||' ROWS DELETED'); END IF; END emp_del; END emp_pack; Invoking a packaged procedure or function from SQL*Plus EXECUTE emp_pack.emp_del(1001)

Packages: Forward Declarations


Identifiers must be declared before referencing them
CREATE OR REPLACE PACKAGE BODY forward_pack IS PROCEDURE award_bonus (. . .) IS BEGIN calc_rating(. . .);--illegal reference END; FUNCTION calc_rating ( . . . ) RETURN NUMBER IS BEGIN RETURN ; END; END forward_pack;

Packages: Forward Declarations (contd)


CREATE OR REPLACE PACKAGE BODY forward_pack IS -- Forward declaration FUNCTION calc_rating(. . .) RETURN NUMBER; PROCEDURE award_bonus (. . .) IS BEGIN calc_rating(. . .);-- Legal reference END; FUNCTION calc_rating ( . . . ) RETURN NUMBER IS BEGIN RETURN ; END; END forward_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

CREATE OR PROCEDURE PROCEDURE PROCEDURE PROCEDURE

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

Das könnte Ihnen auch gefallen