Oracle从表中插入查询

时间:2015-11-11 12:24:38

标签: sql oracle

我有一张看起来像这样的表:

表1

column1 column2 column3
1       2       3 
a       6       9 
b       8       7 

我需要以这种格式将此数据复制到另一个表:

表2

column_name value
column1     1
column1     a
column1     b
column2     2
column2     6
column2     8
column3     3
column3     9
column3     7

有超过100个表,例如table1,有数百万行。我需要在表2中复制它。

所以,我打算写一下:

INSERT INTO table2
(column_name, value)
SELECT 'column1', column1
FROM table1;

INSERT INTO table2
(column_name, value)
SELECT 'column2', column2
FROM table1;

INSERT INTO table2
(column_name, value)
SELECT 'column3', column3
FROM table1;

我有80个这样的列。有没有一种有效的编写方法,这种方法在Oracle中复制数据时也能快速工作?

编辑:更改了示例数据集以包含不同类型的数据。如果我使用unpivot,它会给我:

ORA-01790:表达式必须与对应的表达式具有相同的数据类型

2 个答案:

答案 0 :(得分:5)

使用unpivot

 select * from table1
    unpivot
    (
    "values" for column_names in (column1,column2,column3))

修改:插入上面的语句,以便将数据填充到另一个

create table table2
(
column_names varchar2(1),
"values" varchar2(1)
)
/
    insert into table2
    select * from table1
        unpivot
        (
        "values" for column_names in (column1 as '1',column2 as '2',column3 as '3'))

更新它会将column1替换为1,将column2替换为2等

 select * from table1
        unpivot
        (
        "values" for column_names in (column1 as '1',column2 as '2',column3 as '3'))

答案 1 :(得分:0)

以下是一些解开数据的方法 - 一种是手动方式,一种是使用UNPIVOT关键字。您必须对两者进行测试,以确定哪种方法最适合您的数据集。

with sample_data as (select '1' column1, 2 column2, 3 column3 from dual union all
                     select 'a' column1, 6 column2, 9 column3 from dual union all
                     select 'b' column1, 8 column2, 7 column3 from dual)
select d.id column_id,
       case when d.id = 1 then sd.column1
            when d.id = 2 then to_char(sd.column2)
            when d.id = 3 then to_char(sd.column3)
       end value
from   sample_data sd
       cross join (select level id
                   from   dual
                   connect by level <= 3) d;

COLUMN_NAME VALUE                                   
----------- ----------------------------------------
          1 1                                       
          1 a                                       
          1 b                                       
          2 2                                       
          2 6                                       
          2 8                                       
          3 3                                       
          3 9                                       
          3 7   

with sample_data as (select '1' column1, 2 column2, 3 column3 from dual union all
                     select 'a' column1, 6 column2, 9 column3 from dual union all
                     select 'b' column1, 8 column2, 7 column3 from dual)
select *
from   (select column1,
               to_char(column2) column2,
               to_char(column3) column3
        from   sample_data) sd
unpivot (value for column_id in (column1 as '1', column2 as '2', column3 as '3')) d;

COLUMN_ID VALUE                                   
--------- ----------------------------------------
1         1                                       
2         2                                       
3         3                                       
1         a                                       
2         6                                       
3         9                                       
1         b                                       
2         8                                       
3         7        

在这两个查询中,我都使用了WITH子句(也称为子查询因子,也称为CTE)来模拟包含数据的表。在您的情况下,您不需要使用with子句。

要将数据插入另一个表,您只需在sql语句之前附加insert子句,例如:

insert into other table (column_id, value)
select column_id,
       value
from   (select column1,
               to_char(column2) column2,
               to_char(column3) column3
        from   sample_data) sd
unpivot (value for column_id in (column1 as '1', column2 as '2', column3 as '3')) d;