2017-02-20 26 views
-1
 
ID Date  NAME START_TIME    END_TIME    
1 2/15/2017 A  2/15/20173:40:39 PM  2/15/2017 3:41:17 PM 
2 2/15/2017 B  2/15/20173:40:39 PM  2/15/2017 3:41:17 PM 
3 2/15/2017 C  2/15/20173:40:39 PM  2/15/2017 3:41:17 PM  

我面臨一個問題,那就是我必須用這3條語句回填我的數據庫從2016年1月到今天。 我可以嘗試的一種解決方案是我可以編寫剛剛循環的Java代碼,併爲表創建新日期和新條目,然後使用生成的查詢插入。使用Oracle回填表中的數據

但有沒有什麼辦法可以使用oracle來做到這一點。

+2

你使用MySQL或Oracle? (不要標記不涉及的產品。) – jarlh

+0

A,B和C每一行都有一行?有什麼開始和結束時間? –

+0

@jarlh我使用oracle。 – rkSinghania

回答

0

作爲一個基本概念,類似這樣的事情會做到這一點......您需要爲A,B和C做適應性調整,或者爲每個做適當調整。

with Numbers (NN) as 
(
select 1 as NN 
from dual 
union all 
select NN+1 
from Numbers 
where NN <2000 
) 
insert into MyTable (ID, Date, Name, StartTime, EndTime) 
select NN + 3, -- If repeating, replace the 3 with the max(id) after each run 
     'A', 
     to_date('20170215','YYYYMMDD') - NN, 
     to_date('20170215 154039','YYYYMMDD HH24MISS') - NN, 
     to_date('20170215 154117','YYYYMMDD HH24MISS') - NN 
from NN 
where NN <= 365 
+0

其拋出異常PLS-00182:標識符不能爲空字符串 – rkSinghania

1

這是產生給定的開始和結束日期,你可以簡單地加入到你的名字的列表,以獲得您所需要的日期的常用方式:

insert into yourTable (...)  
with names as (
    select 'A' as name from dual union all 
    select 'B' as name from dual union all 
    select 'C' as name from dual 
), 
dates as (
      select date' 2017-01-01' + level -1 as yourDate 
      from dual 
      connect by date' 2016-01-01' + level -1 <= date '2017-02-20' 
     ) 
select rownum, name, yourDate 
from names 
     cross join dates 

這有稍作修改以更好地適合您的專欄的數量和類型。它是如何工作的一個小例子:

with names as (
    select 'A' as name from dual union all 
    select 'B' as name from dual union all 
    select 'C' as name from dual 
), 
dates as (
      select date' 2017-02-18' + level -1 as yourDate, 
      level as lev 
      from dual 
      connect by date' 2017-02-18' + level -1 <= date '2017-02-20') 
select rownum, name, yourDate, lev 
from names 
     cross join dates 

給出:

ROWNUM N YOURDATE   LEV 
---------- - --------- ---------- 
     1 A 18-FEB-17   1 
     2 B 18-FEB-17   1 
     3 C 18-FEB-17   1 
     4 A 19-FEB-17   2 
     5 B 19-FEB-17   2 
     6 C 19-FEB-17   2 
     7 A 20-FEB-17   3 
     8 B 20-FEB-17   3 
     9 C 20-FEB-17   3 
相關問題