数据源表test03
1 2016-06-01 1
2 2016-06-02 1
3 2016-06-05 1
4 2016-06-04 1
5 2016-06-04 1
procedure代码如下:
create or replace procedure loop_by_date(pbeg_tim in varchar2,--开始日期
pend_tim in varchar2,--结束日期
errmessage out varchar2) is
nCount number(10); --总天数
i_point number(10); --当天
is_zero number(10); --当天是否有记录
begin
nCount := 0;
i_point := 0;
is_zero := 0;
select ROUND(to_date(pend_tim, 'yyyy-mm-dd') -
to_date(pbeg_tim, 'yyyy-mm-dd'))
into nCount
from dual;
delete from test02;
<
loop
select count(*)
into is_zero
from test03
where date1 =
to_char(to_date(pbeg_tim, 'yyyy-mm-dd') + i_point, 'yyyy-mm-dd');
insert into test02
(date01, nccount)
values
(to_char(to_date(pbeg_tim, 'yyyy-mm-dd') + i_point, 'yyyy-mm-dd'),
is_zero);
i_point := i_point + 1;
exit fst_loop when i_point >= nCount;
end loop fst_loop;
--end;
end loop_by_date;
传入参数"2016-06-01"~~"2016-06-10"并执行,结果写入test02为:
1 2016-06-01 1
2 2016-06-02 1
3 2016-06-03 0
4 2016-06-04 2
5 2016-06-05 1
6 2016-06-06 0
7 2016-06-07 0
8 2016-06-08 0
9 2016-06-09 0