最近需要在oracle 中做一个数据统计和分析的功能,需要实pl/sql的语法。在此文中对学习到的注意点进行记录。
- 包的定义和实现
包(package)是oracle中的一种组织结构,包含包头(head)和包体(body) 两部分。包的好处是,可以在包中定义函数和存储过程,组成形成一定业务单元的模块代码。所以,包在抽象层次上就是模块,其目的就是提供了功能模块的组织方式。
包分为包头和包体两部分,有各自的创建语法。在包头中,放置的是包全局变量、存储过程、函数等单元的申明。在包体中,我们需要对包头中申明的对象(函数、存储过程等)进行实现,实现其具体功能。
- 包头
包头的创建语法为
CREATE [OR REPLACE] PACKAGE 包名
{IS|AS}
公有变量定义
公有类型定义
公有游标定义
公有异常定义
函数说明
过程说明
END;
具体实例如下:
CREATE OR REPLACE PACKAGE PKG_2020Main
IS
---packae variable
STATEMENT_TYPE varchar(25);
INTERVAL_COUNT number;
FORECASTDAY_COUNT number;
--procedure init_date; --prepare date
procedure init_combination_item; --prepare combination item
function cal_single(forcest_day Date,param_tni varchar,param_lr varchar,param_frmp varchar,inerval_num number) return number;--cal the forcast value
function cal_holiday_average_volume(param_tni varchar,param_lr varchar,param_frmp varchar,inerval_num number) return number; --cal the hoilday average value
procedure forcast_one_day(forcast_day Date);---forecast one day
procedure RM16_forecast; --entrance
function check_run(run_date Date) return number;--check one on instance
procedure save_xml_file(forcast_day Date);
END PKG_2020Main;
- 包体
包体的创建语法为
CREATE [OR REPLACE] PACKAGE BODY 包名
{IS|AS}
私有变量定义
私有类型定义
私有游标定义
私有异常定义
函数定义
过程定义
具体实例为:
CREATE OR REPLACE PACKAGE BODY PKG_2020Main
IS
PROCEDURE RM16_forecast
IS
begin_day Date;-- start date
current_day Date;-- forcast recrusive date
export_day Date; -- export date
focast_day_increment number :=1;
run_value number:=0;
BEGIN
--package variable
select value into STATEMENT_TYPE from dbp_parameter where code='statement_type';
select to_number(value) into INTERVAL_COUNT from dbp_parameter where code='max_interval_count';
select to_number(value) into FORECASTDAY_COUNT from dbp_parameter where code='forecast_day_count';
--get current date
select trunc(sysdate) into current_day from dual;
export_day:=current_day+1;
begin_day:=current_day;
-- check the instance
run_value:=check_run(begin_day);
if run_value=0 then
DBMS_OUTPUT.PUT_LINE('instance existed,exit!');
return;
end if;
--init
init_combination_item();
-- forcast the days
loop
exit when focast_day_increment>FORECASTDAY_COUNT;
DBMS_OUTPUT.PUT_LINE(current_day);
--day increment
current_day:=current_day+1;
DBMS_OUTPUT.PUT_LINE(current_day);
forcast_one_day(current_day);
focast_day_increment:=focast_day_increment+1;
DBMS_OUTPUT.PUT_LINE('one day end!');
end loop;
--export to xml
save_xml_file(export_day);
END RM16_forecast;
-- PROCEDURE init_date
-- as
-- CURSOR day_cursor IS select distinct(day) from DBP_ADMIN.v_nem_rm16;
-- -- date variable
-- day_current DBP_ADMIN.v_nem_rm16.day%TYPE;
-- day_week varchar(200);
-- holiday_flag number(3);
-- BEGIN
-- select value into STATEMENT_TYPE from dbp_parameter where code='statement_type';
-- select to_number(value) into INTERVAL_COUNT from dbp_parameter where code='max_interval_count';
-- select to_number(value) into FORECASTDAY_COUNT from dbp_parameter where code='forecast_day_count';
-- COMMON.LOG('init date start');
-- delete from history_date;
-- OPEN day_cursor;
-- FETCH day_cursor INTO day_current;
-- WHILE day_cursor%FOUND LOOP
---- --cal the week day
-- day_week:=to_char(day_current,'day','NLS_DATE_LANGUAGE=AMERICAN');
---- --is hoilday
-- holiday_flag:=0;
---- --insert into
-- insert into history_date values(day_current,day_week,holiday_flag);
-- FETCH day_cursor INTO day_current;
-- END LOOP;
-- commit;
-- CLOSE day_cursor;
--
-- END init_date;
PROCEDURE init_combination_item
IS
cursor cursor_tni is select distinct