Oracle 包(Package)

一、爲何要使用包?函數

      答: 在一個大型項目中,可能有不少模塊,而每一個模塊又有本身的過程、函數等。而這些過程、函數默認是放在一塊兒的(如在PL/SQL中,過程默認都是放在一塊兒 的,即Procedures中),這些很是不方便查詢和維護,甚至會發生誤刪除的事件。因此經過使用包就能夠分類管理過程和函數。
     並且在包中還能夠自定義自定義類型,從而在過程和函數中能夠直接使用自定義變量。Oracle中包的概念與JAVA中包的概念很是相似,只是JAVA中的包是爲了分類管理類,可是關鍵字都是package。
     包分兩部分,包規範包體
oop

二、包的使用fetch

(1)定義包規範,包規範可單獨存在。spa

複製代碼
--定義包規範
create or replace package p_stu
as
--定義結構體
type re_stu is record(
rname student.name%type,
rage student.age%type
);
--定義遊標
type c_stu is ref cursor;
--定義函數
function numAdd(num1 number,num2 number)return number;
--定義過程
procedure GetStuList(cid in varchar2,c_st out c_stu);
end;
複製代碼

(2)實現包規範,即包體,名稱必須一致,一樣的遊標定義不能出現,但結構體能夠,方法、過程必須實現。code

複製代碼
--實現包體,名稱一致。
create or replace package body p_stu
as
--遊標和結構體,包規範中已聲明,包體中不用再聲明,直接使用。

--實現方法
function numAdd(num1 number,num2 number)return number
as
num number;
begin
num:=num1+num2;
return num;
end;

--實現過程
procedure GetStuList(cid varchar2,c_st out c_stu)
as
r_stu re_stu; --直接使用包規範中的結構
begin
open c_st for select name,age from student where classid=cid;
-- 若是已經在過程當中遍歷了遊標,在使用這個過程的塊中,將沒有值。
-- loop
-- fetch c_st into r_stu;
-- exit when c_st%notfound;
-- dbms_output.put_line('姓名='||r_stu.rname);
-- end loop;
end;
end;
複製代碼

(3)使用blog

複製代碼
declare
c_stu p_stu.c_stu; --定義包中游標變量
r_stu p_stu.re_stu; --定義包中結構體變量
num number;
begin
--使用及遍歷包中過程返回的結果集
p_stu.GetStuList('C001',c_stu);
loop
fetch c_stu into r_stu;
exit when c_stu%notfound;
dbms_output.put_line('姓名='||r_stu.rname);
end loop;

--使用包中的方法
select p_stu.numAdd(5,6) into num from dual;
dbms_output.put_line('Num='||num);
end;
相關文章
相關標籤/搜索