Oracle --包的應用

pl/sql包的例子
sql


包分爲包頭和包體函數

以存儲過程爲例,包頭中定義存儲過程,包體中實現存儲過程。工具

如上例子:PKG_CONSTANTS 爲常量包     PKG_UTILITY 爲通用函數包日誌

PKG_CONSTANTS包頭部分code

create or replace package PKG_CONSTANTS is
/*Function:
  Created by:xx
  Created on:2015/7/9 11:58:12

  Modification history:
*/
  --動態遊標
  type REF_CURSOR is ref cursor;

  --返回代碼
  C_RTN_SUCCESS CONSTANT NUMBER := '0';  --成功
  C_RTN_FAILURE CONSTANT NUMBER := '1';  --失敗

  --初始版本號
  C_INIT_VERNO CONSTANT NUMBER := 1;

  --狀態2
  C_STATUS2_AVAILABLE   CONSTANT CHAR(1) := '0';  --有效
  C_STATUS2_UNAVAILABLE CONSTANT CHAR(1) := '1';  --無效

  --性別
  C_GENDER_MALE   CONSTANT CHAR(1) := '0';  --男
  C_GENDER_FEMALE CONSTANT CHAR(1) := '1';  --女

end PKG_CONSTANTS;

PKG_CONSTANTS包體部分get

create or replace package body pkg_Constants is
begin
  null;
end pkg_Constants;


PKG_UTILITY包頭部分it

create or replace package PKG_UTILITY is
/*Function:實用工具
  Created by:xxxx
  Created on:2015/7/15 17:40:00
*/
  --獲取系統日期和時間
  procedure sp_getSysDateTime
  (
    o_date out char
   ,o_time out char
  );
  
  --記錄日誌
  procedure sp_writeLog
  (
    i_log_type in t_log.log_type%type  --日誌類型('INFO':普通訊息 'ERROR':錯誤信息)
   ,i_acc_id   in t_log.acc_id%type    --當前操做帳號
   ,i_sp_name  in t_log.sp_name%type   --存儲過程名稱
   ,i_log_desc in t_log.log_desc%type  --具體信息描述
  );
end PKG_UTILITY;

PKG_UTILITY包體部分io

create or replace package body PKG_UTILITY is
  --獲取系統日期和時間
  procedure sp_getSysDateTime
  (
    o_date out char
   ,o_time out char
  )
  is
    l_currentDate date;
  begin
    l_currentDate := sysdate;
    o_date := to_char(l_currentDate, 'YYYYMMDD');
    o_time := to_char(l_currentDate, 'HH24:MI:SS');
  end sp_getSysDateTime;
  
  --記錄日誌
  procedure sp_writeLog
  (
    i_log_type in t_log.log_type%type  --日誌類型('INFO':普通訊息 'ERROR':錯誤信息)
   ,i_acc_id   in t_log.acc_id%type    --當前操做帳號
   ,i_sp_name  in t_log.sp_name%type   --存儲過程名稱
   ,i_log_desc in t_log.log_desc%type  --具體信息描述
  )
  is
    pragma autonomous_transaction;
    
    l_date        char(8);
    l_time        char(8);
    l_log_desc    t_log.log_desc%type;
    l_sqlcode     number;
  begin
    l_sqlcode := sqlcode;
    
    l_log_desc := case i_log_type
                    when 'ERROR' then 
                      case l_sqlcode
                        when 0 then i_log_desc || chr(10) || DBMS_UTILITY.FORMAT_CALL_STACK
                        else i_log_desc || chr(10) || sqlerrm || chr(10) || DBMS_UTILITY.FORMAT_Error_BACKTRACE
                      end
                    else
                      i_log_desc
                  end;
    
    sp_getSysDateTime(l_date, l_time);
    
    insert into t_log(log_id    --日誌序號
                     ,log_date  --日誌日期
                     ,log_time  --日誌時間
                     ,log_type  --日誌類型
                     ,acc_id    --操做帳號
                     ,sp_name   --SP名稱
                     ,err_code  --錯誤代碼
                     ,log_desc  --日誌描述
                     )
               values(seq_log_id.nextval
                     ,l_date
                     ,l_time
                     ,i_log_type
                     ,i_acc_id
                     ,i_sp_name
                     ,l_sqlcode
                     ,l_log_desc
                     );
    
    commit;
    
    exception
      when others then
        insert into t_log(log_id    --日誌序號
                         ,log_date  --日誌日期
                         ,log_time  --日誌時間
                         ,log_type  --日誌類型
                         ,acc_id    --操做帳號
                         ,sp_name   --SP名稱
                         ,err_code  --錯誤代碼
                         ,log_desc  --日誌描述
                         )
                   values(seq_log_id.nextval
                         ,nvl(l_date, to_char(sysdate,'YYYYMDD'))
                         ,nvl(l_time, to_char(sysdate,'HH24:MI:SS'))
                         ,i_log_type
                         ,i_acc_id
                         ,i_sp_name
                         ,l_sqlcode
                         ,'記錄日誌失敗。' || chr(10) || DBMS_UTILITY.FORMAT_Error_BACKTRACE
                         );
  end sp_writeLog;
  
end PKG_UTILITY;

日誌表以下class

相關文章
相關標籤/搜索