Oracle - 两次之间每小时插入一行

时间:2011-09-23 14:25:31

标签: sql oracle plsql

给定两次,我如何在Oracle表中每小时插入一行?

Start Time: 3.00 PM

End Time: 6.00 PM

Name: 'Asdfg'

要生成的预期数据:

Name      StartTime       EndTime
ASDFG       3.00           4.00
ASDFG       4.00,          5.00
ASDFG       5.00           6.00

2 个答案:

答案 0 :(得分:2)

这在没有PL / SQL的情况下,在一个插入中。您可以参数化开始和结束时间并在过程中使用它:

INSERT INTO tbl (NAME, starttime, endtime)
(SELECT 'ASDFG', t1, t2
   FROM (SELECT to_char((to_date('3.00 PM','HH.MI AM')+(LEVEL-1)/24),'HH.MI AM') t1
              , to_char((to_date('3.00 PM','HH.MI AM')+LEVEL/24), 'HH.MI AM') t2
           FROM dual 
        CONNECT BY LEVEL <= (to_date('6.00 PM','HH.MI AM') - to_date('3.00 PM','HH.MI AM')) * 24));

答案 1 :(得分:0)

CurrentTime := StartTime;
WHILE CurrentTime <= EndTime
LOOP
     INSERT INTO MY_TABLE VALUES (CurrentTime);

     CurrentTime  := CurrentTime + (1 / 24);

END LOOP;

COMMIT;

应该做的伎俩(我没试过......)