Oracle
 sql >> Base de Dados >  >> RDS >> Oracle

Precisa inserir registros na tabela oracle para agendamento


Basicamente, trata-se do gerador de linhas técnica, pois você deve gerar número de linhas dependendo de alguns parâmetros de entrada. Aqui está uma opção.

Tabela de destino:
SQL> create table t_schedule (datum date, type varchar2(10));

Table created.

Procedimento que irá gerar linhas; para simplificar, os horários trimestrais são separados dos semanais e diários:
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>

Testes:programação diária:
SQL> exec p_schedule(date '2021-01-01', date '2021-01-25', 'daily');

PL/SQL procedure successfully completed.

Testes:programação semanal:
SQL> exec p_schedule(date '2021-01-01', date '2021-01-25', 'weekly');

PL/SQL procedure successfully completed.

Testes:cronograma trimestral:
SQL> exec p_schedule(date '2021-01-01', date '2021-10-25', 'quarterly');

PL/SQL procedure successfully completed.

Resultado:distinguir as linhas pelo tipo :
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>