Oracle 中包的應用

來源:互聯網
上載者:User

標籤:style   blog   http   io   color   ar   os   sp   資料   

       包由兩個分離的部分組成:包頭(PACKAGE)和包體(PACKAGEBODY)。包頭是包的說明部分,是對外的操作介面,對應用是可見的;包體是包的代碼和實現部分,對應用來說是不可見的黑盒。
       出現在包頭中的稱為公有元素,出現在包體中的稱為私人元素,出現在包體的過程(或函數)中的稱為局部變數。

建立包頭的簡要語句如下:

CREATE [OR REPLACE] PACKAGE 包名{IS|AS}公有變數定義公有類型定義公有遊標定義公有異常定義函數說明過程說明END;

建立包體的簡要文法如下:

CREATE [OR REPLACE] PACKAGE BODY 包名{IS|AS}私人變數定義私人類型定義私人遊標定義私人異常定義函數定義流程定義END;

其它操作:

刪除包頭:DROP PACKAGE 包頭名刪除包體:DROP PACKAGE BODY 包體名重新編譯包頭:ALTER PACKAGE 包名 COMPILE PACKAGE重新編譯包體:ALTER PACKAGE 包名 COMPILE PACKAGE BODY

案例:對學生表infos提供一個增刪改查的包,infos表內容如所示:

包中的內容結構如下:

程式結構 類型 參數 說明
v_infos_count 公有變數   學生總總數量,number類型
p_init 公有過程

p_max number

p_min number

最大值,最小值
p_list_infos 公有過程   顯示學生列表資料
p_add_infos 公有過程

p_stuid infos.stuid%type,
p_stuname infos.stuname%type,
p_gender infos.gender%type,
p_age infos.age%type,
p_seat infos.seat%type,
p_enrolldate infos.enrolldate%type,
p_stuaddress infos.stuaddress%type,
p_classno infos.classno%type

增加一條學生記錄
p_delete_infos 公有過程 p_stuid infos.stuid%type 根據stuid刪除一條學生記錄
p_edit_infos_name 公有過程

p_stuid infos.stuid%type
p_stuname infos.stuname%type

根據stuid修改學生的姓名
v_msg 私人變數   show message
v_max_age 私人變數   max age ,number
v_min_age 私人變數   min age ,number
f_exist_infos 私人函數

p_stuid infos.stuid%type

判斷學生是否存在,

return boolean

p_show_msg 私用程序   show msg

包SQL:

(1)建立包頭create or replace package pck_infosas  --總數量  v_infos_count number;  --初始化操作  procedure p_init(p_max number, p_min number);  --顯示學生列表資料  procedure p_list_infos;  --增加一條學生記錄  procedure p_add_infos(    p_stuid       infos.stuid%type,    p_stuname     infos.stuname%type,    p_gender      infos.gender%type,    p_age         infos.age%type,    p_seat        infos.seat%type,    p_enrolldate  infos.enrolldate%type,    p_stuaddress  infos.stuaddress%type,    p_classno     infos.classno%type);  --刪除一條學生記錄  procedure p_delete_infos(p_stuid infos.stuid%type);  --根據stuid修改學生的姓名  procedure p_edit_infos_name(    p_stuid   infos.stuid%type,    p_stuname infos.stuname%type);end;(2)建立包體create or replace package body pck_infosas  v_msg     varchar2(100);  --show message  v_max_age number;         --max age  v_min_age number;         --min age    --判斷學生是否存在  function f_exist_infos(p_stuid infos.stuid%type)  return boolean;    --show msg  procedure p_show_msg;    --初始化操作  procedure p_init(p_max number, p_min number)  as  begin    select count(stuid) into v_infos_count from infos;    v_max_age:=p_max;    v_min_age:=p_min;    v_msg:=‘init finished!‘;    p_show_msg;  end p_init;    --顯示資訊  procedure p_show_msg  as  begin    dbms_output.put_line(v_msg);  end p_show_msg;   --判斷學生是否存在  function f_exist_infos(p_stuid infos.stuid%type)  return boolean  as    v_num number;  begin    select count(stuid) into v_num from infos where stuid=p_stuid;    if v_num=1 then      return true;    else      return false;    end if;  end f_exist_infos;    --顯示學生列表資料  procedure p_list_infos  as    v_infos_record infos%rowtype;    cursor cur_infos is select * from infos;  begin    open cur_infos;    loop      fetch cur_infos into v_infos_record;      exit when cur_infos%notfound;      dbms_output.put_line(‘stuid:‘||v_infos_record.stuid);    end loop;    close cur_infos;  end p_list_infos;    --增加一條學生記錄  procedure p_add_infos(    p_stuid       infos.stuid%type,    p_stuname     infos.stuname%type,    p_gender      infos.gender%type,    p_age         infos.age%type,    p_seat        infos.seat%type,    p_enrolldate  infos.enrolldate%type,    p_stuaddress  infos.stuaddress%type,    p_classno     infos.classno%type)  as  begin    if not f_exist_infos(p_stuid) then      insert into infos(stuid,stuname,gender,age,seat,enrolldate,stuaddress,classno)        values(p_stuid,p_stuname,p_gender,p_age,p_seat,p_enrolldate,p_stuaddress,p_classno);      commit;      v_infos_count:=v_infos_count+1;    else      v_msg:=‘already exist!‘;    end if;  end p_add_infos;    --刪除一條學生記錄  procedure p_delete_infos(p_stuid infos.stuid%type)  as  begin    if f_exist_infos(p_stuid) then      delete from infos where stuid=p_stuid;      commit;      v_infos_count:=v_infos_count-1;    else      v_msg:=‘not exist infos!‘;    end if;  end p_delete_infos;     --根據stuid修改學生的姓名  procedure p_edit_infos_name(    p_stuid   infos.stuid%type,    p_stuname infos.stuname%type)  as  begin    if f_exist_infos(p_stuid) then      update infos set stuname=p_stuname where stuid=p_stuid;      commit;    else      v_msg:=‘not exists infos‘;    end if;  end p_edit_infos_name;    end;

 

Oracle 中包的應用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.