Compoments of a PL/SQL Package
Package Specification是必须要有的,Package body是可以没有的(bodyless);但是如果你在package specification中声明了cursor,subprogram等,那么就必须要有package body,来实现上面的cursor、subprogram声明.
Internal and External Visibility of a Package`s Components
Developing PL/SQL Packages:Overview
Creating the Package Specificiation:Using the CREATE PACKAGE Satement
CREATE [OR REPLACE] PACKAGE pakcage_name IS|AS public type and variable declarations subprogram specifications END [package_name]
- The OR REPLACE option drops and re-creates the pakcae specification
- Variables declared in the package specification are initialized to NULL by default.
- All the constructs declared in a package specification are visiable to uses who are granted privileges on the package.
Example of a Package Specification:comm_pkg
--The package specification with a pulbic variable and a public procedure --that are accessible from outside the package. CREATE OR REPLACE PACKAGE comm_pkg IS v_std_comm NUMBER :=0.10; --initialized to 0.10 PROCEDURE reset_comm(p_new_comm NUMBER);--注意,在这里声明的是什么样,在body里面实现的时候,也必须一模一样,或者叫做完全一样 END comm_pkg; /
- V_STD_COMM is a public global variable initalized to 0.10
- RESET_COMM is a public procedure used to reset the standard commission based on some business rules.it is implemented in the packaged body.
SQL> BEGIN 2 DBMS_OUTPUT.PUT_LINE(comm_pkg.v_std_comm); 3 END; 4 / .1 PL/SQL procedure successfully completed.
Creating the Package Body
CREATE [OR REPLACE] PACKAGE BODY package_name IS|AS private type and variable declarations subprogram bodies [BEGIN iniialization statements] END [package_name]
- The OR REPLACE option drops and re-creates the package body.
- Identifiers defined in the package body are private and not visible outside the package body.
- All private constructs must be declared before they are referenced.
- Public constructs are visible to the package body.
Demo:创建Package comm_pkg(完整Demo)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
--The package specification with a pulbic variable and a public procedure --that are accessible from outside the package. CREATE OR REPLACE PACKAGE comm_pkg IS v_std_comm NUMBER :=0.10; --initialized to 0.10 PROCEDURE reset_comm(p_new_comm NUMBER); END comm_pkg; / CREATE OR REPLACE PACKAGE BODY comm_pkg IS FUNCTION validate(p_comm NUMBER) RETURN BOOLEAN IS v_max_comm employees.commission_pct%TYPE; BEGIN SELECT MAX(commission_pct) INTO v_max_comm FROM employees; RETURN (p_comm BETWEEN 0.0 AND v_max_comm); END validate; PROCEDURE reset_comm(p_new_comm NUMBER) IS BEGIN --v_std_comm := p_new_comm; --reset public var IF validate(p_new_comm) THEN v_std_comm := p_new_comm; --rest public var DBMS_OUTPUT.PUT_LINE(v_std_comm); ELSE RAISE_APPLICATION_ERROR(-20210,'Bad Commission'); END IF; END reset_comm; END comm_pkg; / BEGIN comm_pkg.reset_comm(0.5); END; /
Invoking the Package Subprograms:Examples
方法一:直接在包内调用
PROCEDURE reset_comm(p_new_comm NUMBER) IS BEGIN IF validate(p_new_comm) THEN v_std_comm := p_new_comm; --rest public var DBMS_OUTPUT.PUT_LINE(v_std_comm); ELSE RAISE_APPLICATION_ERROR(-20210,'Bad Commission'); END IF; END reset_comm;
方法二:Invoke a package procedure from SQL*PLUS
EXECUTE comm_pkg.reset_comm(0.15);
方法三:Invoke a package procedure in a different schema:
EXECUTE scott.comm_pkg.reset_comm(0.15);
Creating and Using Bodiless Packages
Bodiless Packages的一般作用是:定义一些常量.
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
CREATE OR REPLACE PACKAGE global_constants IS c_mile_2_kilo CONSTANT NUMBER := 1.6093; c_kilo_2_mile CONSTANT NUMBER := 0.6214; c_yard_2_meter CONSTANT NUMBER := 0.9144; c_meter_2_yard CONSTANT NUMBER := 1.0936; END global_constants; / SET SERVEROUT ON BEGIN DBMS_OUTPUT.PUT_LINE('20 miles =' || 20 * global_constants.c_mile_2_kilo || 'km'); END; / CREATE FUNCTION mtr2yrd(p_m NUMBER) RETURN NUMBER IS BEGIN RETURN (p_m * global_constants.c_meter_2_yard); END mtr2yrd; / EXECUTE DBMS_OUTPUT.PUT_LINE(mtr2yrd(1));
Package Instantiation and Initialization
- When a session references a package itme,Oracle Database instantiates the package for that session.Every session that references a package has its own instantiation of that package.
- When Oracle Database instantiates a package,it initializes it.Initialization includes whichever of the following are applicable:
- -Assigning initial values to public constants.
- -Assigning initial values to public variables whose declarations specify them.
- -Executing the initializing part of the package body.