在Oracle中将整个表复制到另一个列除外

时间:2016-03-15 15:59:31

标签: oracle

在下面的代码中,我试图将表的内容复制到另一个表,除了一列。对于该特定列,我想从第三个表中插入一个值,其中来自不同表的两列匹配。

insert into PlaneKeyLookup (launchtime, landingtime, flightduration,
                 planeid, pilot1init, pilot2init, launchtype, kmsflown) values
((select launchtime from NewFlightsTransformed),
(select landingtime from NewFlightsTransformed),
(select flightduration from NewFlightsTransformed),
(select dp.id from D_Plane dp where exists
   (select nf.planeregistration from NewFlightsTransformed 
    where dp.registration = nf.planeregistration)),
(select pilot1init from NewFlightsTransformed),
(select pilot2init from NewFlightsTransformed),
(select launchtype from NewFlightsTransformed),
(select kmsflown from NewFlightsTransformed));

有什么建议吗?

1 个答案:

答案 0 :(得分:0)

您想要的SQL更像是:

insert into PlaneKeyLookup (launchtime, landingtime, flightduration,
                 planeid, pilot1init, pilot2init, launchtype, kmsflown)
select launchtime, 
       landingtime, 
       flightduration,
       (select dp.id from D_Plane dp where...),
       pilot1init, 
       pilot2init, 
       launchtype, 
       kmsflown
  from NewFlightsTransformed;

我不确切知道对于planeid的查询应该是什么,但是它必须返回1行,该行获取与其一起使用的NewFlightsTransformed行的ID。如果需要(可能),它可以与外部查询相关

(select dp.id from D_Plane dp 
  where dp.xxx = NewFlightsTransformed.xxx
    and ...)

也许:

(select dp.id from D_Plane dp where dp.registration = nf.planeregistration)

即。

insert into PlaneKeyLookup (launchtime, landingtime, flightduration,
                 planeid, pilot1init, pilot2init, launchtype, kmsflown)
select launchtime, 
       landingtime, 
       flightduration,
       (select dp.id from D_Plane dp where dp.registration = nf.planeregistration),
       pilot1init, 
       pilot2init, 
       launchtype, 
       kmsflown
  from NewFlightsTransformed nf;