我有一个脚本来填充上一个(2016年),当前(2017年)和明年(2018年)。该脚本旨在最初运行以填充表。由于它会填充以前的日期,因此只能运行一次。如何填充将来的日期(2019年)?
insert into my_date
SELECT TO_NUMBER (TO_CHAR (mydate, 'yyyymmdd')) AS my_date_id,
mydate AS datetime_start,
mydate + 1 - 1/86400 AS datetime_end,
TO_CHAR (mydate, 'dd-MON-yyyy') AS date_value,
TO_NUMBER (TO_CHAR (mydate, 'D')) AS day_of_week,
TO_CHAR (mydate, 'Day') AS day_of_week_name,
TO_CHAR (mydate, 'DY') AS day_of_week_name_short,
TO_NUMBER (TO_CHAR (mydate, 'DD')) AS day_of_month,
TRUNC (mydate) - TRUNC (mydate, 'Q') + 1 AS day_of_quarter,
TO_NUMBER (TO_CHAR (mydate, 'DDD')) AS day_of_year,
CASE WHEN TO_NUMBER (TO_CHAR (mydate, 'D')) IN (1, 7) THEN 1
ELSE 0
END AS weekend_flag,
TO_NUMBER (TO_CHAR (mydate, 'W')) AS week_in_month,
TO_NUMBER (TO_CHAR (mydate, 'WW')) AS week_in_year,
TRUNC(mydate, 'w') AS week_start_date,
TRUNC(mydate, 'w') + 7 - 1/86400 AS week_end_date,
TO_CHAR (mydate, 'MM') AS month_value,
TO_CHAR (mydate, 'Month') AS month_name,
TO_CHAR (mydate, 'MON') AS month_name_short,
TRUNC (mydate, 'mm') AS month_start_date,
LAST_DAY (TRUNC (mydate, 'mm')) + 1 - 1/86400 AS month_end_date,
TO_NUMBER ( TO_CHAR( LAST_DAY (TRUNC (mydate, 'mm')), 'DD')) AS days_in_month,
CASE WHEN mydate = LAST_DAY (TRUNC (mydate, 'mm')) THEN 1
ELSE 0
END AS last_day_of_month_flag,
TO_CHAR (mydate, 'yyyy') AS year_value,
'YR' || TO_CHAR (mydate, 'yyyy') AS year_name,
'YR' || TO_CHAR (mydate, 'yy') AS year_name_short,
TRUNC (mydate, 'Y') AS year_start_date,
ADD_MONTHS (TRUNC (mydate, 'Y'), 12) - 1/86400 AS year_end_date,
ADD_MONTHS (TRUNC (mydate, 'Y'), 12) - TRUNC (mydate, 'Y') AS days_in_year
FROM ( SELECT TRUNC (ADD_MONTHS (SYSDATE, -12), 'yy') - 1 + LEVEL AS mydate
FROM dual
CONNECT BY LEVEL <= (SELECT TRUNC (ADD_MONTHS (SYSDATE, 24), 'yy')
- TRUNC (ADD_MONTHS (SYSDATE, -12), 'yy')
FROM DUAL
)
);
在FROM子句中将需要进行更改以避免现有记录。我该如何实现?
答案 0 :(得分:0)
管道功能可能会在这里为您提供帮助。
create or replace type date_tbl_t as table of date;
/
create or replace function all_dates ( max_year_in in integer )
return date_tbl_t
pipelined
as
date_l date;
offset_l pls_integer := 0;
year_l integer;
begin
if date_l is null
then
date_l := sysdate;
end if;
year_l := extract ( year from date_l );
while year_l <= max_year_in
loop
pipe row(date_l);
date_l := date_l + 1;
year_l := extract ( year from date_l );
end loop;
return;
end all_dates;
/
select
to_char(x.object_value, 'yyyymmdd') as my_date_id,
x.object_value as datetime_start
from table ( all_dates (2019) ) x;
/
create or replace type date_tbl_t as table of date;
/
create or replace function all_dates ( max_year_in in integer )
return date_tbl_t
pipelined
as
date_l date;
offset_l pls_integer := 0;
year_l integer;
begin
if date_l is null
then
date_l := sysdate;
end if;
year_l := extract ( year from date_l );
while year_l <= max_year_in
loop
pipe row(date_l);
date_l := date_l + 1;
year_l := extract ( year from date_l );
end loop;
return;
end all_dates;
/
select
to_char(x.object_value, 'yyyymmdd') as my_date_id,
x.object_value as datetime_start
from table ( all_dates (2019) ) x;
/