oracle中的程式包

2021-09-30 15:29:50 字數 1867 閱讀 2882

一 程式包的基本概念

程式包可將若干函式或者儲存過程組織起來,作為乙個物件進行儲存。程式包通常由兩部分構成,規範(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'

四 呼叫程式包中的函式或者儲存過程

呼叫函式(sql window)

select pkg_staff.get_staff_string() as result from dual

呼叫儲存過程(command window)

begin

pkg_staff.delete_staff(1);

end;/

Oracle基礎 程式包篇

oracle資料庫中,程式包是一系列相關儲存過程和函式的集合,通過程式包可以簡化語句塊的編寫,便於對各種過程 函式進行分類。宣告程式包中各種函式或者儲存過程,只有在包頭中宣告過的才可以通過包直接對外部提供。包頭中宣告的函式或儲存過程必須在包體中實現,並且引數型別和返回值型別必須一致。可以使用cons...

Oracle中子程式和程式包中的函式

oracle中的函式 oracle中的函式是子程式和程式包中的一部分,函式與過程相似,是資料庫中儲存的已命名pl sql程式塊。函式的主要特徵是它必須返回乙個值。建立函式時通過return子句指定函式返回值資料型別。在函式的任何地方,都可以通過 return 表示式 語句從函式返回,這裡的 表示式 ...

Oracle資料庫中的程式包練習

1.在乙個包中定義兩個過載的方法,兩個方法作用都是求乙個數到另乙個數的和並將和返回,方法申明如下 包頭 create orreplace package pack1 asfunction getsum endnumber int return int function getsum endnumbe...