ORACLE的程式包1-程式包的基礎

來源:互聯網
上載者:User

標籤:

程式包簡析

  oracle中的程式包簡析    一 程式包的基本概念
    程式包可將若干函數或者預存程序組織起來,作為一個對象進行儲存。程式包通常由兩部分構成,規範(specification)和主體(body)。

    程式包也可以包含常量和變數,包中的所有函數和預存程序都可以使用這些變數或者常量。

   程式包是對相關過程、函數、變數、遊標和異常等對象的封裝 程式包由規範和主體兩部分組成

    二 規範
    1 建立規範(SQL視窗)
    create or replace package pkg_staff as
    staffString varchar2(500);
    stafftAge number:=18;
    function get_staff_string return varchar2;
    procedure insert_staff(in_staff_id in number,in_staff_name in varchar2);
    procedure update_staff(in_staff_id in number);
    procedure delete_staff(in_staff_id in number);
    end pkg_staff;
    2 在資料字典中查看程式包規範的資訊
    select object_name,object_type,status from user_objects
    where lower(OBJECT_NAME) = ‘pkg_staff‘
    三 主體
    所謂規範,就像物件導向編程中的介面,該規範的主體必須實現該規範的所有方法。Oracle會自動尋找與主體同名的規範,看是否全部實現了該規範函數或者預存程序。若沒有,則編譯錯誤。
    1 建立主體
    create or replace package body pkg_staff as
    function get_staff_string return varchar2 as
    begin
    return ‘staff‘;
    end get_staff_string;
    procedure insert_staff(in_staff_id in number,in_staff_name in varchar2) as
    begin
    insert into staff values (in_staff_id,in_staff_name);
    end insert_staff;
    procedure update_staff(in_staff_id in number) as
    begin
    update staff set name = ‘xy‘ where num = in_staff_id;
    end update_staff;
    procedure delete_staff(in_staff_id in number) as
    begin
    delete from staff where num = ‘1‘;
    end delete_staff;
    end pkg_staff;
    2 在資料字典中查看程式包主體的資訊
    select object_name,object_type,status from user_objects
    where lower(OBJECT_NAME) = ‘pkg_staff‘
    www.2cto.com
    四 調用程式包中的函數或者預存程序
    調用函數(SQL window)
    select pkg_staff.get_staff_string() as result from dual
    調用預存程序(Command window)
    begin
    pkg_staff.delete_staff(1);
    end;
    /

ORACLE的程式包1-程式包的基礎

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.