如何将日历项插入到 Oracle 数据库记录中?

How to insert a Calender Items into Oracle DB records?

我正在尝试将一整年的日历插入到 ORACLE 数据库记录中 我的专栏是

----------------------------------------------------------------
| [FULL_DATE] | [DAY] | [MONTH_NAME] | [MONTH_NUMBER] | [YEAR] |
----------------------------------------------------------------
Function 
(
 @DATEFROM AS DATE
 @DATETO AS DATE 
) RETURNS DATE
AS 
BEGIN 
set @datefrom = '01/01/1995' 
set @dateto = '31/12/1996' 
while(@datefrom < @dateto) 
BEGIN set @datefrom = DATEADD(day , 1 , @datefrom) 
insert into SHEMA.DIM_TIME_TABLE ( FULL_DATE , DAY , MONTH , YEAR ) select DAY(GETDATE(@datefrom)) , DATENAME(MONTH , @datefrom), MONTH(GETDATE(@datefrom)) , YEAR(GETDATE(@datefrom))
END 
RETURN
END

预期:

---------------------------------------------------------------
 01 / 01 /1995 | 01 | JAN  | 01 | 1995
---------------------------------------------------------------
 02 / 01 /1995 | 02 | JAN  | 01 | 1995  
---------------------------------------------------------------
 03 / 01 /1996 | 03 | JAN  | 01 | 1995

在Oracle中,您可以使用递归查询生成日期系列,然后在外部查询中生成预期的列:

create table dim_time_table as
select 
    dt full_date,
    extract(day from dt) day,
    to_char(dt, 'month') month_name,
    extract(month from dt) month_number,
    extract(year from dt) year
from (
    select to_date('1995-01-01', 'yyyy-mm-dd') + level - 1 as dt 
    from dual 
    connect by 
        to_date('1995-01-01', 'yyyy-mm-dd') + level 
        <= to_date('1997-01-01', 'yyyy-mm-dd')
)

Demo on DB Fiddle:

FULL_DATE | DAY | MONTH_NAME | MONTH_NUMBER | YEAR
:-------- | --: | :--------- | -----------: | ---:
01-JAN-95 |   1 | january    |            1 | 1995
02-JAN-95 |   2 | january    |            1 | 1995
03-JAN-95 |   3 | january    |            1 | 1995
04-JAN-95 |   4 | january    |            1 | 1995
05-JAN-95 |   5 | january    |            1 | 1995
06-JAN-95 |   6 | january    |            1 | 1995
07-JAN-95 |   7 | january    |            1 | 1995
...