2013-07-12 62 views
1

我試圖做一個插入成基於選擇從表1表2,但我不能得到正確的語法。如下例所示,table1中的列名將驅動插入到table2中的PD_NO列中的值。有人能幫忙嗎?SQL - 插入使用列名作爲值

表1:

  (1)  (2)  (3)  (4)  (5)  (6) 
| SEQ | PD_01 | PD_02 | PD_03 | PD_04 | PD_05 | PD_06 | 
|-----+-------+-------+-------+-------+-------+-------| 
| 632 | 10000 | 0 | 500 | 0 | 20000 | 0 | 

表2:

| SEQ | PD_NO | AMT | 
|-----+-------+-------| 
| 632 | 1 | 10000 | 
|-----+-------+-------| 
| 632 | 3 | 500 | 
|-----+-------+-------| 
| 632 | 5 | 20000 | 
|-----+-------+-------| 

我知道,如果我工作的另一個方向(插入的表2到表1內容),我可以做類似如下:

INSERT INTO table1 
SELECT 
     seq, 
     SUM (CASE WHEN pd_no = 1 THEN amt ELSE 0 END) p01_amt, 
     SUM (CASE WHEN pd_no = 2 THEN amt ELSE 0 END) p02_amt, 
     SUM (CASE WHEN pd_no = 3 THEN amt ELSE 0 END) p03_amt, 
     SUM (CASE WHEN pd_no = 4 THEN amt ELSE 0 END) p04_amt, 
     SUM (CASE WHEN pd_no = 5 THEN amt ELSE 0 END) p05_amt, 
     SUM (CASE WHEN pd_no = 6 THEN amt ELSE 0 END) p06_amt 
FROM table2; 
+0

請與品牌RDBMS你」的標記您的問題重新使用。例如。 'sql-server','mysql','oracle'等。 –

回答

3

這是一個典型的問題,爲此甲骨文11提供UNPIVOT子句中使用查詢:

insert into table2(seq, pd_no, amt) 
select seq, pd_no, amt 
from (select * 
      from table1 
      unpivot (amt for pd_no in (pd_01 as 1, pd_02 as 2, pd_03 as 3, pd_04 as 4, pd_05 as 5, pd_06 as 6)) 
     ); 
+0

Tomasz,謝謝你。這正是我所期待的。 – thefreeline

0

insert ... select from以1:1的行爲基礎工作:「選擇」選項卡中的一行數據le進入'insert'表的一行。您試圖從源表中取一行,並將其轉換爲目標表中的多行。這不是直接可能的。你必須運行多個插入/選擇,一個是要打出每場:

INSERT INTO table2 (SEQ, PD_NO, AMT) SELECT SEQ, PD_01, AMT FROM table1 
INSERT INTO table2 (SEQ, PD_NO, AMT) SELECT SEQ, PD_02, AMT FROM table1 
INSERT INTO table2 (SEQ, PD_NO, AMT) SELECT SEQ, PD_03, AMT FROM table1 
INSERT INTO table2 (SEQ, PD_NO, AMT) SELECT SEQ, PD_04, AMT FROM table1 
etc... 
2

純SQL可以這樣做:

INSERT INTO table2 (SEQ , PD_NO, AMT) 
SELECT SEQ, 1 as pd_no, PD_01 FROM Table1 
UNION ALL 
SELECT SEQ, 2 as pd_no, PD_02 FROM Table1 
UNION ALL 
SELECT SEQ, 3 as pd_no, PD_03 FROM Table1 
UNION ALL 
SELECT SEQ, 4 as pd_no, PD_04 FROM Table1 
UNION ALL 
SELECT SEQ, 5 as pd_no, PD_05 FROM Table1 
UNION ALL 
SELECT SEQ, 6 as pd_no, PD_06 FROM Table1 

有些數據庫已經優化讀取源表僅一次的命令(上述查詢讀源表6次),例如,在ORACLE:

INSERT ALL 
INTO table2 (SEQ , PD_NO, AMT) VALUES (seq, 1, PD_01) 
INTO table2 (SEQ , PD_NO, AMT) VALUES (seq, 2, PD_02) 
INTO table2 (SEQ , PD_NO, AMT) VALUES (seq, 3, PD_03) 
INTO table2 (SEQ , PD_NO, AMT) VALUES (seq, 4, PD_04) 
INTO table2 (SEQ , PD_NO, AMT) VALUES (seq, 5, PD_05) 
INTO table2 (SEQ , PD_NO, AMT) VALUES (seq, 6, PD_06) 
SELECT * FROM table1 
+0

INSERT ALL工作完美。感謝您的反饋。 – thefreeline

0
CREATE TABLE 1to6 (i INT PRIMARY KEY); 
INSERT INTO 1to6 (i) VALUES (1), (2), (3), (4), (5), (6); 

INSERT INTO table2 (seq, pd_no, amt) 
SELECT seq, i, 
CASE(i) 
WHEN 1 THEN pd_01 
WHEN 2 THEN pd_02 
WHEN 3 THEN pd_03 
WHEN 4 THEN pd_04 
WHEN 5 THEN pd_05 
WHEN 6 THEN pd_06 
END 
FROM table1 CROSS JOIN 1to6 

重新評論:顯然,Oracle不支持多行INSERT語法(儘管該語法是標準的SQL-99)。您可以一次插入一行:

INSERT INTO 1to6 (i) VALUES (1); 
INSERT INTO 1to6 (i) VALUES (2); 
INSERT INTO 1to6 (i) VALUES (3); 
INSERT INTO 1to6 (i) VALUES (4); 
INSERT INTO 1to6 (i) VALUES (5); 
INSERT INTO 1to6 (i) VALUES (6); 
+0

這個選項看起來很乾淨。但是,當我嘗試在第2行插入值時,出現「SQL命令沒有正確結束」的錯誤。刪除「,(2),(3)等等」工作正常,並給我一張表(i)和值1.任何想法? – thefreeline