Packages
在練習中 , 你很少創建一個獨立的存儲 function 或 procedure. 相反 , 你會使用 package. 什麼是 package? Package 是組織相關的 function 和 procedure 包含 在一起 , 就像創建一個圖書館 , 但在 PL/SQL 術語中稱為 package. 當你 建立 Procedure 或 Function 後會 保存 在 Oracle 的 Procedure 與Function 目錄裡 , 如果我有一個 Procedure 功能與已存在的 Procedure 類似 , 但又有點不一樣 , Oracle 又不允許在 Procedure 目錄下建立相同名稱的 Procedure, 這時候就要靠 Package 來把相同名稱的 Procedure 分開 . Package 也有分類的意思 , 將相關功能模組封裝在同一個 Package 內可提高聚合性 .
PL/SQL Package 有兩部分 :
1. Package specification
2. Package body
Package Specification 定義了此 Package 內所有的成員 , 例如 :Procedure 與 Function, 這裡定義的Procedure 與 Function 只要擺定義即可不需要放實作的內容 , 在 Specification 的成員都是對外公開 Public.
Package Body 為放置成員 Spec 的詳細實作區 , 在建立 Package 時要先有 Specification 才可以有 Body, 你可以將 Spec 與 Body 分成兩個檔案撰寫也可以撰寫在同一個檔案裡 .
Package Specification 語法 :
CREATE [OR REPLACE] PACKAGE <package_name> AS
-- one or more: constant, cursor, function, procedure, or variable declarations
END <package_name>;
/
Create package spec.
以下 Package spec 例子 , 包含了 5 個 functions, 但只有 declare 並沒有運行 code, code 在 package body.
create or replace package date_ as d_max constant date:= to_date('99991231','YYYYMMDD'); d_min constant date:= to_date('19900101','YYYYMMDD'); -- 常數關鍵字 constant. 它需要一個初始值 , 並且不允許被改變該值 . function end_of_day( input1in date) return date; function get_max return date; function get_min return date; function random ( starting_year in number, ending_year in number) return date ; function start_of_day( input2 in date) return date; end date_; / |
Create package body:
create or replace package body date_ as function end_of_day(input1 in date) return date as begin return to_date( to_char(input1, 'YYYYMMDD')||'235959','SYYYYMMDDHH24MISS'); end end_of_day; function get_max return date as begin returnd_max; end get_max; function get_min return date as begin returnd_min; end get_min; function random (starting_year in number, ending_year in number) return date as d_random date; n_day number; n_month number; n_year number; begin n_year := round(dbms_random. value(starting_year,ending_year),0); n_month := round(dbms_random.value(1,12),0); n_day := round(dbms_random.value(1,31),0); d_random := to_date(lpad(to_char(n_year),4,'0')|| lpad(to_char(n_month),2,'0')|| lpad(to_char(n_day),2,'0'),'YYYYMMDD'); exception when others then pl( SQLERRM ); end; return d_random; end random; function start_of_day(input2 in date) return date as begin return trunc(input2); endstart_of_day; enddate_; / |
執行效果 :
SQL> alter session set nls_date_format='YYYYMMDDHH24MISS';
Session altered.
SQL> select date_.end_of_day(sysdate) from dual;
DATE_.END_OF_D
--------------
20160725235959
SQL> select date_.get_max from dual;
GET_MAX
--------------
99991231000000
SQL> select date_.get_min from dual;
GET_MIN
--------------
19900101000000
SQL> select date_.random(1994,2014) from dual;
DATE_.RANDOM(1
--------------
19960809000000
SQL> select date_.start_of_day('20160101235959') from dual;
DATE_.START_OF
--------------
20160101000000
I t’s Your turn to Create a package
還記得較早前創建的 to_number2 function 嗎 ? 用它來創建你的 package 吧 . 條款如下 :
1. Package 名稱 NUMBER_
2. 用 select 來測試 package.
PL/SQL BLOCK 語法比較
ANONYMOUS [DECLARE] | CREATE FUNCTION | CREATE PROCEDURE | CREATE PACKAGE | CREATE PACKAGE BODY |
- | [parameters] | [parameters] | - | - |
- | RETURN | - | - | - |
[declaration section] | [declaration section] | [declaration section] | [declaration section] | [declaration section] |
BEGIN | BEGIN | BEGIN | - | BEGIN |
executable section | executable section | executable section | - | executable section |
[EXCEPTION] | [EXCEPTION] | [EXCEPTION] | - | [EXCEPTION] |
[exception handling] | [exception handling] | [exception handling] | - | [exception handling] |
END; | END; | END; | END; | END; |
/ | / | / | / | / |