Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
147 views
in Technique[技术] by (71.8m points)

How to insert data into oracle database table using PL SQL which includes 365 days of the year

I already have a table built in oracle. I'm trying to insert some data that looks like this:

I want the data to look like this

But, I can't seem to be able to add the DATE together with the ID, YEAR and INDICATOR.

I manage to get the DATE from 1/1/2019 7:00:00 PM - 12/31/2019 7:00:00 PM.

Example of only date

CODE:

INSERT INTO TABLE(DATE)
select to_date('01-01-2019 7:00:00 PM', 'DD-MM-YYYY HH:MI:SS PM') + rownum -1 dt from dual 
connect by level <= to_date('05-01-2019 7:00:00 PM', 'DD-MM-YYYY HH:MI:SS PM') - 
                    to_date('01-01-2019 7:00:00 PM', 'DD-MM-YYYY HH:MI:SS PM') + 1;

When I exclude DATE, it looks like this:

Example of ID, YEAR and INDICATOR

CODE:

INSERT INTO TABLE (ID, YEAR, INDICATOR)
Values (sequ.nextval, '2019', 'X') ;

I tried to combine the two codes but it doesn't work. Is there any other ways I can do to make it works? Or I'm doing it the wrong way? The only change data is the DATE because I need it to be 365 days of 2019. YEAR and INDICATOR remain the same for all data.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Is this what you want?

insert into table(id, "date", "year", indicator)
    select rownum, dt, extract(year from dt), 'X'
    from (select to_date('01-01-2019 7:00:00 PM', 'DD-MM-YYYY HH:MI:SS PM') + rownum - 1 as dt
          from dual 
          connect by level <= to_date('05-01-2019 7:00:00 PM', 'DD-MM-YYYY HH:MI:SS PM') - 
                        to_date('01-01-2019 7:00:00 PM', 'DD-MM-YYYY HH:MI:SS PM') + 1
         ) t;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...