我找到了相关问题的以下答案。
with calendar as(
select :startdate + rownum - 1 as day
from dual
connect by rownum < :enddate - startdate
)
select rownum as "S.No", to_date(day,'dd_mm_yyyy') as "Cal_Dt", to_char(day,'day') as "DayName"
from calendar
此SQL为一系列日期生成许多日期和相关信息。有些正文可以告诉我如何将其包装到SQL语句中,以便可以将select(所有行)中的内容插入到另一个表中。
答案 0 :(得分:1)
如果要插入数据的表已存在,请使用INSERT INTO
:
with calendar as (
select :startdate + rownum - 1 as day
from dual
connect by rownum < :enddate - startdate )
insert into anothertable (no, dt, dayname)
select rownum as "S.No", to_date(day,'dd_mm_yyyy') as "Cal_Dt", to_char(day,'day') as "DayName"
from calendar
或使用 SELECT INTO
语法创建新表:
with calendar as (
select :startdate + rownum - 1 as day
from dual
connect by rownum < :enddate - startdate )
select rownum as "S.No", to_date(day,'dd_mm_yyyy') as "Cal_Dt", to_char(day,'day') as "DayName" into newtable
from calendar
击> <击> 撞击> 编辑 - 实际上评论是完全正确的 - 没有考虑Oracle :-)这是从Oracle中的SQL语句创建表的正确方法:
with calendar as (
select :startdate + rownum - 1 as day
from dual
connect by rownum < :enddate - startdate )
CREATE TABLE newtable
AS
(select rownum as "S.No", to_date(day,'dd_mm_yyyy') as "Cal_Dt", to_char(day,'day') as "DayName"
from calendar)
答案 1 :(得分:1)
伪列LEVEL
也可以正常工作
insert into tgt_tbl(id,dt,dy)
select distinct level,:startdate+level-1,to_char(:startdate+level-1,'Day') from dual
connect by level<=(:enddate-:startdate)+1
答案 2 :(得分:0)
CREATE TABLE DATES_TABLE
(
Date_Field DATE
);
INSERT INTO DATES_TABLE
(
Date_Field
)
SELECT ROWNUM - 1 + TO_DATE('01-Jun-2004','dd-mon-yyyy') Date_Field
FROM all_objects
WHERE ROWNUM < TO_DATE('30-Jun-2004','dd-mon-yyyy') - TO_DATE('01-Jun-2004','dd-mon-yyyy') + 2;
尝试更改这些日期值以获得所需范围内的日期值