Oracle月表转总表
来源:互联网 发布:c语言求平方根函数 编辑:程序博客网 时间:2024/06/05 03:35
create or replace procedure ExportBill is
m_Sql varchar2(2048);
m_cherror varchar2(256);
v_nowtime number(10);
v_starttime number(10);
v_tmpriqi varchar2(32);
v_tablename varchar2(32);
v_day varchar2(32);
v_nday number(3);
v_hour varchar2(32);
v_nhour number(3);
begin
select to_number((sysdate -To_date('1970-01-01 08-00-00', 'yyyy-mm-dd hh24-mi-ss')) * 24 * 60 * 60) into v_nowtime from dual;
v_starttime := v_nowtime - 86400 * 5;
SELECT TO_CHAR(v_nowtime/86400 + TO_DATE('19700101080000','YYYYMMDDHH24MISS'),'YYYYMMDDHH24MISS') into v_tmpriqi FROM DUAL;
v_day := substr(v_tmpriqi,7,2);
v_nday := to_number(v_day);
v_hour := substr(v_tmpriqi,9,2);
v_nhour := to_number(v_hour);
v_tmpriqi := substr(v_tmpriqi,0,6);
v_tablename := 'mp_bill' || v_tmpriqi;
if (v_nhour = 12) then
--删除100天前的数据
m_Sql := 'delete from mp_bill where datetime < ' || to_char(v_nowtime-86400*100);
begin
execute immediate m_Sql;
commit;
exception
when others then
m_cherror := substr(SQLERRM, 1, 200);
end;
end if;
if (v_nday < 5) then
--取本月数据
m_Sql := 'merge into mp_bill mb using (
select a.mp_id,a.power_type,a.datetime,a.total_bill,a.apex_bill,a.peak_bill,a.flat_bill,a.valley_bill,a.quality_code from ' || v_tablename || ' a where a.datetime > ' || to_char(v_starttime) || ') ins
on (mb.mp_id = ins.mp_id and mb.power_type = ins.power_type and mb.datetime = ins.datetime)
when matched then
update set mb.total_bill = ins.total_bill,mb.apex_bill = ins.apex_bill,mb.peak_bill = ins.peak_bill,mb.flat_bill = ins.flat_bill,mb.valley_bill = ins.valley_bill,mb.quality_code = ins.quality_code
when not matched then
insert (mb.mp_id,mb.power_type,mb.datetime,mb.total_bill,mb.apex_bill,mb.peak_bill,mb.flat_bill,mb.valley_bill,mb.quality_code)
values (ins.mp_id,ins.power_type,ins.datetime,ins.total_bill,ins.apex_bill,ins.peak_bill,ins.flat_bill,ins.valley_bill,ins.quality_code)';
begin
execute immediate m_Sql;
commit;
exception
when others then
m_cherror := substr(SQLERRM, 1, 200);
end;
--取上月数据
SELECT TO_CHAR(v_starttime/86400 + TO_DATE('19700101080000','YYYYMMDDHH24MISS'),'YYYYMMDDHH24MISS') into v_tmpriqi FROM DUAL;
v_tmpriqi := substr(v_tmpriqi,0,6);
v_tablename := 'mp_bill' || v_tmpriqi;
m_Sql := 'merge into mp_bill mb using (
select a.mp_id,a.power_type,a.datetime,a.total_bill,a.apex_bill,a.peak_bill,a.flat_bill,a.valley_bill,a.quality_code from ' || v_tablename || ' a where a.datetime > ' || to_char(v_starttime) || ') ins
on (mb.mp_id = ins.mp_id and mb.power_type = ins.power_type and mb.datetime = ins.datetime)
when matched then
update set mb.total_bill = ins.total_bill,mb.apex_bill = ins.apex_bill,mb.peak_bill = ins.peak_bill,mb.flat_bill = ins.flat_bill,mb.valley_bill = ins.valley_bill,mb.quality_code = ins.quality_code
when not matched then
insert (mb.mp_id,mb.power_type,mb.datetime,mb.total_bill,mb.apex_bill,mb.peak_bill,mb.flat_bill,mb.valley_bill,mb.quality_code)
values (ins.mp_id,ins.power_type,ins.datetime,ins.total_bill,ins.apex_bill,ins.peak_bill,ins.flat_bill,ins.valley_bill,ins.quality_code)';
begin
execute immediate m_Sql;
commit;
exception
when others then
m_cherror := substr(SQLERRM, 1, 200);
end;
else
--取本月数据
m_Sql := 'merge into mp_bill mb using (
select a.mp_id,a.power_type,a.datetime,a.total_bill,a.apex_bill,a.peak_bill,a.flat_bill,a.valley_bill,a.quality_code from ' || v_tablename || ' a where a.datetime > ' || to_char(v_starttime) || ') ins
on (mb.mp_id = ins.mp_id and mb.power_type = ins.power_type and mb.datetime = ins.datetime)
when matched then
update set mb.total_bill = ins.total_bill,mb.apex_bill = ins.apex_bill,mb.peak_bill = ins.peak_bill,mb.flat_bill = ins.flat_bill,mb.valley_bill = ins.valley_bill,mb.quality_code = ins.quality_code
when not matched then
insert (mb.mp_id,mb.power_type,mb.datetime,mb.total_bill,mb.apex_bill,mb.peak_bill,mb.flat_bill,mb.valley_bill,mb.quality_code)
values (ins.mp_id,ins.power_type,ins.datetime,ins.total_bill,ins.apex_bill,ins.peak_bill,ins.flat_bill,ins.valley_bill,ins.quality_code)';
begin
execute immediate m_Sql;
commit;
exception
when others then
m_cherror := substr(SQLERRM, 1, 200);
end;
end if;
end ExportBill;
m_Sql varchar2(2048);
m_cherror varchar2(256);
v_nowtime number(10);
v_starttime number(10);
v_tmpriqi varchar2(32);
v_tablename varchar2(32);
v_day varchar2(32);
v_nday number(3);
v_hour varchar2(32);
v_nhour number(3);
begin
select to_number((sysdate -To_date('1970-01-01 08-00-00', 'yyyy-mm-dd hh24-mi-ss')) * 24 * 60 * 60) into v_nowtime from dual;
v_starttime := v_nowtime - 86400 * 5;
SELECT TO_CHAR(v_nowtime/86400 + TO_DATE('19700101080000','YYYYMMDDHH24MISS'),'YYYYMMDDHH24MISS') into v_tmpriqi FROM DUAL;
v_day := substr(v_tmpriqi,7,2);
v_nday := to_number(v_day);
v_hour := substr(v_tmpriqi,9,2);
v_nhour := to_number(v_hour);
v_tmpriqi := substr(v_tmpriqi,0,6);
v_tablename := 'mp_bill' || v_tmpriqi;
if (v_nhour = 12) then
--删除100天前的数据
m_Sql := 'delete from mp_bill where datetime < ' || to_char(v_nowtime-86400*100);
begin
execute immediate m_Sql;
commit;
exception
when others then
m_cherror := substr(SQLERRM, 1, 200);
end;
end if;
if (v_nday < 5) then
--取本月数据
m_Sql := 'merge into mp_bill mb using (
select a.mp_id,a.power_type,a.datetime,a.total_bill,a.apex_bill,a.peak_bill,a.flat_bill,a.valley_bill,a.quality_code from ' || v_tablename || ' a where a.datetime > ' || to_char(v_starttime) || ') ins
on (mb.mp_id = ins.mp_id and mb.power_type = ins.power_type and mb.datetime = ins.datetime)
when matched then
update set mb.total_bill = ins.total_bill,mb.apex_bill = ins.apex_bill,mb.peak_bill = ins.peak_bill,mb.flat_bill = ins.flat_bill,mb.valley_bill = ins.valley_bill,mb.quality_code = ins.quality_code
when not matched then
insert (mb.mp_id,mb.power_type,mb.datetime,mb.total_bill,mb.apex_bill,mb.peak_bill,mb.flat_bill,mb.valley_bill,mb.quality_code)
values (ins.mp_id,ins.power_type,ins.datetime,ins.total_bill,ins.apex_bill,ins.peak_bill,ins.flat_bill,ins.valley_bill,ins.quality_code)';
begin
execute immediate m_Sql;
commit;
exception
when others then
m_cherror := substr(SQLERRM, 1, 200);
end;
--取上月数据
SELECT TO_CHAR(v_starttime/86400 + TO_DATE('19700101080000','YYYYMMDDHH24MISS'),'YYYYMMDDHH24MISS') into v_tmpriqi FROM DUAL;
v_tmpriqi := substr(v_tmpriqi,0,6);
v_tablename := 'mp_bill' || v_tmpriqi;
m_Sql := 'merge into mp_bill mb using (
select a.mp_id,a.power_type,a.datetime,a.total_bill,a.apex_bill,a.peak_bill,a.flat_bill,a.valley_bill,a.quality_code from ' || v_tablename || ' a where a.datetime > ' || to_char(v_starttime) || ') ins
on (mb.mp_id = ins.mp_id and mb.power_type = ins.power_type and mb.datetime = ins.datetime)
when matched then
update set mb.total_bill = ins.total_bill,mb.apex_bill = ins.apex_bill,mb.peak_bill = ins.peak_bill,mb.flat_bill = ins.flat_bill,mb.valley_bill = ins.valley_bill,mb.quality_code = ins.quality_code
when not matched then
insert (mb.mp_id,mb.power_type,mb.datetime,mb.total_bill,mb.apex_bill,mb.peak_bill,mb.flat_bill,mb.valley_bill,mb.quality_code)
values (ins.mp_id,ins.power_type,ins.datetime,ins.total_bill,ins.apex_bill,ins.peak_bill,ins.flat_bill,ins.valley_bill,ins.quality_code)';
begin
execute immediate m_Sql;
commit;
exception
when others then
m_cherror := substr(SQLERRM, 1, 200);
end;
else
--取本月数据
m_Sql := 'merge into mp_bill mb using (
select a.mp_id,a.power_type,a.datetime,a.total_bill,a.apex_bill,a.peak_bill,a.flat_bill,a.valley_bill,a.quality_code from ' || v_tablename || ' a where a.datetime > ' || to_char(v_starttime) || ') ins
on (mb.mp_id = ins.mp_id and mb.power_type = ins.power_type and mb.datetime = ins.datetime)
when matched then
update set mb.total_bill = ins.total_bill,mb.apex_bill = ins.apex_bill,mb.peak_bill = ins.peak_bill,mb.flat_bill = ins.flat_bill,mb.valley_bill = ins.valley_bill,mb.quality_code = ins.quality_code
when not matched then
insert (mb.mp_id,mb.power_type,mb.datetime,mb.total_bill,mb.apex_bill,mb.peak_bill,mb.flat_bill,mb.valley_bill,mb.quality_code)
values (ins.mp_id,ins.power_type,ins.datetime,ins.total_bill,ins.apex_bill,ins.peak_bill,ins.flat_bill,ins.valley_bill,ins.quality_code)';
begin
execute immediate m_Sql;
commit;
exception
when others then
m_cherror := substr(SQLERRM, 1, 200);
end;
end if;
end ExportBill;
阅读全文
0 0
- Oracle月表转总表
- Oracle???
- oracle
- oracle
- oracle
- oracle
- oracle...
- oracle
- oracle
- ORACLE
- Oracle
- ORACLE
- Oracle
- Oracle
- oracle
- oracle
- oracle
- ORACLE
- HDU 6195 数学
- Annotation--反射与Annotation
- 互联网产品都是怎么做推广的?(app产品推广)
- 一万块的iPhone X来了!你错过的发布会亮点都在这里
- 集群环境ssh免密码登录设置
- Oracle月表转总表
- .build_release/lib/libcaffe.so: undefined reference to cv::imread(cv::String const&, int)’ .
- fiddler https 抓包
- JAVA泛型<?>和<T>的区别
- Zookeeper学笔记 --- Zookeeper中watcher机制
- JAVA的环境变量配置(JDK1.6.0_45)
- POJ 3384 Feng Shui (计算几何+半平面交)
- 搜索引擎优化和内容策略
- Catlike渲染教程之Shader基础