茫茫網海中的冷日 - 對這文章發表回應
茫茫網海中的冷日
         
茫茫網海中的冷日
發生過的事,不可能遺忘,只是想不起來而已!
 恭喜您是本站第 1672347 位訪客!  登入  | 註冊
主選單

Google 自訂搜尋

Goole 廣告

隨機相片
IMG_00047.jpg

授權條款

使用者登入
使用者名稱:

密碼:


忘了密碼?

現在就註冊!

對這文章發表回應

發表限制: 非會員 可以發表

發表者: 冷日 發表時間: 2019/5/30 14:36:39

學習初級Oracle PL/SQL (三)

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;
/
/
/
/
/



原文出處:Alan Yeung 的學習 Blog: 學習初級Oracle PL/SQL (三)
內容圖示
url email imgsrc image code quote
樣本
bold italic underline linethrough   












 [詳情...]
validation picture

注意事項:
預覽不需輸入認證碼,僅真正發送文章時才會檢查驗證碼。
認證碼有效期10分鐘,若輸入資料超過10分鐘,請您備份內容後,重新整理本頁並貼回您的內容,再輸入驗證碼送出。

選項

Powered by XOOPS 2.0 © 2001-2008 The XOOPS Project|