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