zoukankan      html  css  js  c++  java
  • PL/SQL高级Creating Packages (01)

    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)

    Create Package secification and body
    --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的一般作用是:定义一些常量.

    Bodiless Packages
    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.
  • 相关阅读:
    strspn实现 查找第一个不在指定字符串中出现的字符
    strstr 的实现 即 字符串中查找子字符串第一次出现的位置
    strcspn
    将字符串中的大写字母变成小写字母
    strrchr 字符在字符串中最后一次出现的位置
    strchr 字符c在字符串中出现的位置 实现
    qsort库函数 排序
    strncmp实现
    strcmp实现
    ACM/ICPC题目输入情形及其处理方法
  • 原文地址:https://www.cnblogs.com/arcer/p/3047659.html
Copyright © 2011-2022 走看看