Oracle INSERT INTO使用SELECT

时间:2014-11-04 08:52:05

标签: sql oracle

我需要在Oracle中复制一些列行。

我有一个表customer_address和一列address_type,其中1是送货地址,2是送货地址等。

我需要复制这些列:

customer_no
customer_name
customer_email

所以例如目前

 CUSTOMER_NO ADDRESS_TYPE CUSTOMER_NAME CUSTOMER_EMAIL
 ----------- ------------ ------------- --------------
 40          1            Some Customer email@somecustomer.com

但我想要的是

 CUSTOMER_NO ADDRESS_TYPE CUSTOMER_NAME CUSTOMER_EMAIL
 ----------- ------------ ------------- --------------
 40          1            Some Customer email@somecustomer.com
 40          2            Some Customer email@somecustomer.com

有可能吗?

更新

感谢@a_horse_with_no_name的回答,但是当我查询表时结果看起来很奇怪,我得到两次列名,这是正确的还是我错过了导致这种情况的东西?

例如

select customer_no, address_type, customer_name, customer_email
from   customer_address
where  customer_no = 40;

显示

 CUSTOMER_NO ADDRESS_TYPE CUSTOMER_NAME CUSTOMER_EMAIL
 ----------- ------------ ------------- --------------
 40          1            Some Customer email@somecustomer.com
 CUSTOMER_NO ADDRESS_TYPE CUSTOMER_NAME CUSTOMER_EMAIL
 ----------- ------------ ------------- --------------
 40          2            Some Customer email@somecustomer.com

由于

1 个答案:

答案 0 :(得分:1)

insert into customer_address (customer_no, address_type, customer_name, customer_email)
select customer_no,
       2,
       customer_name, 
       customer_email
from customer_address
where customer_id = 40
  and address_type = 1;
commit;

有关INSERT语法的完整说明,请参阅手册:
http://docs.oracle.com/cd/E11882_01/server.112/e41084/statements_9014.htm#SQLRF01604