【问题标题】:Need to insert records to oracle table for scheduling需要向oracle表中插入记录进行调度
【发布时间】:2024-01-21 07:45:01
【问题描述】:

请帮我解决这个要求。有从日期,到日期和时间表类型(每日,每周,每月,每年)作为参数。我需要使用提到的 3 个参数向 oracle 表插入行以进行调度。

Eg-  
        from date=01/01/2021
        to date = 25/01/2021
        schedule type=Daily

那么表格应该插入以下行

    Scheduled Dates
    01/01/2021
    02/01/2021
    03/01/2021
    .
    24/01/2021
    25/01/2021

Eg-    

  when from date=01/01/2021
    to date = 25/01/2021
    schedule type=Weekly

那么表格应该插入以下行

Scheduled Dates
01/01/2021
08/01/2021
15/01/2021
22/01/2021

【问题讨论】:

标签: oracle date insert schedule


【解决方案1】:

基本上,它是关于 行生成器 技术,因为您应该根据某些输入参数生成 行数。这是一种选择。

目标表:

SQL> create table t_schedule (datum date, type varchar2(10));

Table created.

生成行的过程;为简单起见,季度计划与每周和每日计划分开:

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>

测试:每日时间表:

SQL> exec p_schedule(date '2021-01-01', date '2021-01-25', 'daily');

PL/SQL procedure successfully completed.

测试:每周安排:

SQL> exec p_schedule(date '2021-01-01', date '2021-01-25', 'weekly');

PL/SQL procedure successfully completed.

测试:季度安排:

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

PL/SQL procedure successfully completed.

结果:按类型区分行:

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>

【讨论】:

  • 您好,非常感谢您的回答,您能否告诉我如何生成季度?
  • 完成;看看吧。
  • 非常感谢
  • 在 Stack Overflow 上的“谢谢”最好通过支持/接受有用的答案来表达,@TDApower。
  • 您好,如果您还可以每月和每年添加到程序中,那就太好了