? ? ? ?包由兩個(gè)分離的部分組成:包頭(PACKAGE)和包體(PACKAGEBODY)。包頭是包的說明部分,是對(duì)外的操作接口,對(duì)應(yīng)用是可見的;包體是包的代碼和實(shí)現(xiàn)部分,對(duì)應(yīng)用來說是不可見的黑盒。
? ? ? ?出現(xiàn)在包頭中的稱為公有元素,出現(xiàn)在包體中的稱為私有元素,出現(xiàn)在包體的過程(或函數(shù))中的稱為局部變量。
創(chuàng)建包頭的簡(jiǎn)要語句如下:
CREATE [OR REPLACE] PACKAGE 包名 {IS | AS} 公有變量定義 公有類型定義 公有游標(biāo)定義 公有異常定義 函數(shù)說明 過程說明 END;
創(chuàng)建包體的簡(jiǎn)要語法如下:
CREATE [OR REPLACE] PACKAGE BODY 包名 {IS | AS} 私有變量定義 私有類型定義 私有游標(biāo)定義 私有異常定義 函數(shù)定義 過程定義 END;
其它操作:
刪除包頭:
DROP PACKAGE 包頭名
刪除包體:
DROP PACKAGE BODY 包體名
重新編譯包頭:
ALTER PACKAGE 包名 COMPILE PACKAGE
重新編譯包體:
ALTER PACKAGE 包名 COMPILE PACKAGE BODY
案例:對(duì)學(xué)生表infos提供一個(gè)增刪改查的包,infos表內(nèi)容如下圖所示:
包中的內(nèi)容結(jié)構(gòu)如下:
程序結(jié)構(gòu) | 類型 | 參數(shù) | 說明 |
v_infos_count | 公有變量 | ? | 學(xué)生總總數(shù)量,number類型 |
p_init | 公有過程 |
p_max number p_min number |
最大值,最小值 |
p_list_infos | 公有過程 | ? | 顯示學(xué)生列表數(shù)據(jù) |
p_add_infos | 公有過程 |
p_stuid infos.stuid%type,
|
增加一條學(xué)生記錄 |
p_delete_infos | 公有過程 | p_stuid infos.stuid%type | 根據(jù)stuid刪除一條學(xué)生記錄 |
p_edit_infos_name | 公有過程 |
p_stuid infos.stuid%type
|
根據(jù)stuid修改學(xué)生的姓名 |
v_msg | 私有變量 | ? | show message |
v_max_age | 私有變量 | ? | max age ,number |
v_min_age | 私有變量 | ? | min age ,number |
f_exist_infos | 私有函數(shù) |
p_stuid infos.stuid%type |
判斷學(xué)生是否存在, return boolean |
p_show_msg | 私有過程 | ? | show msg |
包SQL:
( 1 )創(chuàng)建包頭 create or replace package pck_infos as -- 總數(shù)量 v_infos_count number ; -- 初始化操作 procedure p_init(p_max number , p_min number ); -- 顯示學(xué)生列表數(shù)據(jù) procedure p_list_infos; -- 增加一條學(xué)生記錄 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); -- 刪除一條學(xué)生記錄 procedure p_delete_infos(p_stuid infos.stuid % type); -- 根據(jù)stuid修改學(xué)生的姓名 procedure p_edit_infos_name( p_stuid infos.stuid % type, p_stuname infos.stuname % type); end ; ( 2 )創(chuàng)建包體 create or replace package body pck_infos as v_msg varchar2 ( 100 ); -- show message v_max_age number ; -- max age v_min_age number ; -- min age -- 判斷學(xué)生是否存在 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; -- 判斷學(xué)生是否存在 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; -- 顯示學(xué)生列表數(shù)據(jù) 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; -- 增加一條學(xué)生記錄 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; -- 刪除一條學(xué)生記錄 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; -- 根據(jù)stuid修改學(xué)生的姓名 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 ;
?
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】元
