2017-04-07 47 views
1

我有兩列每個月的表,這樣從列轉換數據行上PLSQL甲骨文

表1:

_____________________________________________________________________ 
|item | jan_p | jan_r | feb_p | feb_r | commpany|   
|______|___________|___________|____________|___________|____________ 
|1294 | 1234232 | 2323322 | 1242122 | 532323 | E987 | 
|______|___________|___________|____________|___________|___________| 

我需要將數據轉移到一個新的,更標準化的表是這樣的:

表2:

________________________________________________ 
| item | month | cost_p | cost_r | company | 
|_______|_______|_________|_________|__________| 
| 1294 | 1 |1234232 | 2323322| E987 | 
| 1295 | 2 |1242122 | 532323 | E987 | 
|_______|_______|_________|_________|__________| 

看到圖像https://ibb.co/cDM91Q

我編寫了一個循環遍歷第一個表格嵌套循環中插入行每個月這樣的:

for reg in (select * from table1) loop 
    for x in 1..12 loop 
     case x 
      when 1 then 
       insert into table2(..., mes , ...) values(... , x, ...) 
      when 2 then 
       insert into table2(..., mes , ...) values(... , x, ...) 

這是工作,但我只是想知道,如果還有另一種方法可以實現相同的結果。

+1

請編輯您的問題,並只使用英語,或在這裏發佈您的問題:https://es.stackoverflow.com/ – dparoli

+0

沒有sabe nada de plsql cierto? –

回答

0

「我只是想知道是否有另一種方式來實現這一目標」

是的,有。這是INSERT ALL語法的理想用例。

insert all 
    into table2 (item, month, cost_p, cost_r, commpany) 
     values (item, 1, jan_p, jan_r, commpany) 
    into table2 (item, month, cost_p, cost_r, commpany) 
     values (item, 2, feb_p, feb_r, commpany) 
    into table2 (item, month, cost_p, cost_r, commpany) 
     values (item, 3, mar_p, mar_r, commpany) 
    into table2 (item, month, cost_p, cost_r, commpany) 
     values (item, 4, apr_p, apr_r, commpany) 
    into table2 (item, month, cost_p, cost_r, commpany) 
     values (item, 5, may_p, may_r, commpany) 
    into table2 (item, month, cost_p, cost_r, commpany) 
     values (item, 6, jun_p, jun_r, commpany) 
    .... 
select * from table1 
/

多表插入允許我們做一些真正整潔的事情,但仍然使用set操作。 Find out more