標籤:
程式包簡析
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-程式包的基礎