在Oracle PL/SQL中用过程插入数据

i2byvkas  于 2023-03-17  发布在  Oracle
关注(0)|答案(2)|浏览(138)

我必须创建一个过程,用2000-01-01到2100-01-01的日期填充表MYTABLE

CREATE TABLE MYTABLE (
ID VARCHAR2(4), 
DATE1 DATE, 
QUARTAL VARCHAR2(4), 
MONTH NUMBER, 
WEEKDAY VARCHAR2(2) 
);

ID必须采用yyyy格式,日期1为2000-01-01到2100-01-01之间的日期,季度为Q1、Q2、Q3或Q4,月份为1-12,工作日显示为是或否。

but5z9lq

but5z9lq1#

这就是你需要的行生成器。下面是一个例子。
日期格式(仅用于了解 * 什么是什么 *):

SQL> alter session set nls_date_Format = 'yyyy-mm-dd';

Session altered.

CTE在您指定的日期之间创建日历:

SQL> with temp (datum) as
  2    (select date '2000-01-01' + level - 1
  3     from dual
  4     connect by level <= date '2100-01-01' - date '2000-01-01' + 1
  5    )
  6  select min(datum), max(datum) from temp;

MIN(DATUM) MAX(DATUM)
---------- ----------
2000-01-01 2100-01-01

返回值的代码,正如您所解释的。我只提取了一个小子集,只是为了说明查询返回的值:

SQL> with temp (datum) as
  2    (select date '2000-01-01' + level - 1
  3     from dual
  4     connect by level <= date '2100-01-01' - date '2000-01-01' + 1
  5    )
  6  select extract(year from datum) as id,
  7    datum as date1,
  8    to_char(datum, 'q') as quartal,
  9    extract(month from datum) as month,
 10    case when to_char(datum, 'dy', 'nls_date_language = english') in ('sat', 'sun') then 'N'
 11         else 'Y'
 12    end as weekday
 13  from temp
 14  where datum between date '2000-03-25' and date '2000-04-05'
 15  order by datum;

        ID DATE1      Q      MONTH W
---------- ---------- - ---------- -
      2000 2000-03-25 1          3 N
      2000 2000-03-26 1          3 N
      2000 2000-03-27 1          3 Y
      2000 2000-03-28 1          3 Y
      2000 2000-03-29 1          3 Y
      2000 2000-03-30 1          3 Y
      2000 2000-03-31 1          3 Y
      2000 2000-04-01 2          4 N
      2000 2000-04-02 2          4 N
      2000 2000-04-03 2          4 Y
      2000 2000-04-04 2          4 Y
      2000 2000-04-05 2          4 Y

12 rows selected.

SQL>

我将把插入目标表的工作留给您。

uidvcgyl

uidvcgyl2#

下面的代码将创建完整的日历表,从您想要的任何一天开始,生成您想要的天数。有2个占位符应该正确放置。在代码中,它们已经设置为生成从01.01.2000到31.12.2099的日历

--
-- Create calendar table
--
-- Drop table if it alreadyy exist
--
--DROP TABLE CALENDAR_TABLE;
--
CREATE TABLE CALENDAR_TABLE AS
    WITH 
        base_calendar AS
            (
                SELECT CurrDate         AS Day_ID,
                1                       AS Day_Time_Span,
                CurrDate                AS Day_End_Date,
                TO_CHAR(CurrDate,'Day') AS Week_Day_Full,
                TO_CHAR(CurrDate,'DY')  AS Week_Day_Short,
                TO_NUMBER(TRIM(leading '0' FROM TO_CHAR(CurrDate,'D')))                 AS Day_Num_of_Week,
                TO_NUMBER(TRIM(leading '0' FROM TO_CHAR(CurrDate,'DD')))                AS Day_Num_of_Month,
                TO_NUMBER(TRIM(leading '0' FROM TO_CHAR(CurrDate,'DDD')))               AS Day_Num_of_Year,
                UPPER(TO_CHAR(CurrDate,'Mon') || '-' || TO_CHAR(CurrDate,'YYYY'))       AS Month_ID,
                TO_CHAR(CurrDate,'Mon') || ' ' || TO_CHAR(CurrDate,'YYYY')              AS Month_Short_Desc,
                RTRIM(TO_CHAR(CurrDate,'Month')) || ' ' || TO_CHAR(CurrDate,'YYYY')     AS Month_Long_Desc,
                TO_CHAR(CurrDate,'Mon')    AS Month_Short,
                TO_CHAR(CurrDate,'Month')  AS Month_Long,
                TO_NUMBER(TRIM(leading '0' FROM TO_CHAR(CurrDate,'MM')))    AS Month_Num_of_Year,
                'Q' || UPPER(TO_CHAR(CurrDate,'Q') || '-' || TO_CHAR(CurrDate,'YYYY'))  AS Quarter_ID,
                TO_NUMBER(TO_CHAR(CurrDate,'Q'))    AS Quarter_Num_of_Year,
                CASE
                  WHEN TO_NUMBER(TO_CHAR(CurrDate,'Q')) <= 2
                  THEN 1
                  ELSE 2
                END                         AS half_num_of_year,
                CASE
                  WHEN TO_NUMBER(TO_CHAR(CurrDate,'Q')) <= 2
                  THEN 'H'
                    || 1
                    || '-'
                    || TO_CHAR(CurrDate,'YYYY')
                  ELSE 'H'
                    || 2
                    || '-'
                    || TO_CHAR(CurrDate,'YYYY')
                END                      AS half_of_year_id,
                TO_CHAR(CurrDate,'YYYY') AS Year_ID
              FROM
                (
                    SELECT level n,
                        -- Calendar starts at the day after the date in next line
                        TO_DATE('31/12/1999','DD/MM/YYYY') + NUMTODSINTERVAL(level,'DAY') CurrDate
                    FROM dual
                        -- Tthe number of days to be added to the table in next line
                    CONNECT BY level <= 36525
                )
            )
SELECT  day_id,
        day_time_span,
        day_end_date,
        week_day_full,
        week_day_short,
        day_num_of_week,
        day_num_of_month,
        day_num_of_year,
        month_id,
        COUNT(*) OVER (PARTITION BY month_id)    AS Month_Time_Span,
        MAX(day_id) OVER (PARTITION BY month_id) AS Month_End_Date,
        month_short_desc,
        month_long_desc,
        month_short,
        month_long,
        month_num_of_year,
        quarter_id,
        COUNT(*) OVER (PARTITION BY quarter_id)    AS Quarter_Time_Span,
        MAX(day_id) OVER (PARTITION BY quarter_id) AS Quarter_End_Date,
        quarter_num_of_year,
        half_num_of_year,
        half_of_year_id,
        COUNT(*) OVER (PARTITION BY half_of_year_id)    AS Half_Year_Time_Span,
        MAX(day_id) OVER (PARTITION BY half_of_year_id) AS Half_Year_End_Date,
        year_id,
        COUNT(*) OVER (PARTITION BY year_id)    AS Year_Time_Span,
        MAX(day_id) OVER (PARTITION BY year_id) AS Year_End_Date
FROM base_calendar
ORDER BY day_id

表创建后测试

Select Min(DAY_ID) "START_DATE", Max(DAY_ID) "END_DATE", Count(DAY_ID) "NUM_OF_DATES" 
From Calendar_Table

START_DATE END_DATE  NUM_OF_DATES
---------- --------- ------------
01-JAN-00  31-DEC-99        36525

相关问题