Grundlæggende handler det om rækkegeneratoren teknik, som du skal generere antal rækker afhængig af visse inputparametre. Her er en mulighed.
Måltabel:
SQL> create table t_schedule (datum date, type varchar2(10));
Table created.
Procedure som vil generere rækker; For nemheds skyld er kvartalsskemaer adskilt fra ugentlige og daglige:
SQL> create or replace procedure p_schedule
2 (par_date_from in date, par_date_to in date, par_type in varchar2)
3 is
4 begin
5 if par_type in ('daily', 'weekly') then
6 insert into t_schedule (datum, type)
7 select par_date_from + (case when par_type = 'weekly' then 7 else 1 end) * (level - 1) as datum,
8 par_type
9 from dual
10 connect by level <= ceil((par_date_to - par_date_from + 1) /
11 case when par_type = 'weekly' then 7 else 1 end);
12 elsif par_type in ('monthly', 'quarterly', 'annually') then
13 insert into t_schedule (datum, type)
14 select add_months(par_date_from, case when par_type = 'monthly' then 1
15 when par_type = 'quarterly' then 3
16 when par_type = 'annually' then 12
17 end * (level - 1)) as datum,
18 par_type
19 from dual
20 connect by level <= ceil(months_between(par_date_to, par_date_from) /
21 case when par_type = 'monthly' then 1
22 when par_type = 'quarterly' then 3
23 when par_type = 'annually' then 12
24 end);
25 end if;
26 end;
27 /
Procedure created.
SQL>
Test:daglig tidsplan:
SQL> exec p_schedule(date '2021-01-01', date '2021-01-25', 'daily');
PL/SQL procedure successfully completed.
Test:ugentlig tidsplan:
SQL> exec p_schedule(date '2021-01-01', date '2021-01-25', 'weekly');
PL/SQL procedure successfully completed.
Test:kvartalsvis tidsplan:
SQL> exec p_schedule(date '2021-01-01', date '2021-10-25', 'quarterly');
PL/SQL procedure successfully completed.
Resultat:skelne rækker efter typen :
SQL> select * from t_schedule order by type, datum;
DATUM TYPE
---------- ----------
01.01.2021 daily
02.01.2021 daily
03.01.2021 daily
04.01.2021 daily
05.01.2021 daily
06.01.2021 daily
07.01.2021 daily
08.01.2021 daily
09.01.2021 daily
10.01.2021 daily
11.01.2021 daily
12.01.2021 daily
13.01.2021 daily
14.01.2021 daily
15.01.2021 daily
16.01.2021 daily
17.01.2021 daily
18.01.2021 daily
19.01.2021 daily
20.01.2021 daily
21.01.2021 daily
22.01.2021 daily
23.01.2021 daily
24.01.2021 daily
25.01.2021 daily
01.01.2021 quarterly
01.04.2021 quarterly
01.07.2021 quarterly
01.10.2021 quarterly
01.01.2021 weekly
08.01.2021 weekly
15.01.2021 weekly
22.01.2021 weekly
33 rows selected.
SQL>