如何将数据列从一个Oracle数据库合并到另一个数据库?

时间:2019-03-21 04:56:11

标签: sql oracle

我有两个表 <android.support.v7.widget.CardView card_view:cardBackgroundColor="@color/green" xmlns:card_view="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="wrap_content" card_view:cardCornerRadius="10dp" card_view:cardElevation="5dp" card_view:cardUseCompatPadding="true"> <android.support.v7.widget.CardView android:layout_margin="3dp" android:id="@+id/card_view" xmlns:card_view="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="wrap_content" card_view:cardBackgroundColor="@color/white" card_view:cardCornerRadius="10dp" > </android.support.v7.widget.CardView> </android.support.v7.widget.CardView> Order

Intend表以及示例数据:

Order

oano code slno --------------- 1808 ln 1 1808 lk 2 1808 lc 3 1809 ki 1 1809 dl 2 表以及示例数据:

Intend

我想将Intendno Oano code slno ------------------------ I/1 1808 ln I/1 1808 lc I/2 1809 ki I/3 1809 dl 表中的slno数据合并到Order表中,如下所示:

Intend

我怎样才能最好地做到这一点?

3 个答案:

答案 0 :(得分:1)

使用merge的一种可能的解决方案:

MERGE INTO Intend target
USING (select oano, code, max(slno) slno
           from "Order"
           group by oano, code
      ) source
ON
(target.code = source.code and target.Oano = source.Oano 
WHEN MATCHED THEN UPDATE
    SET target.slno= source.slno;

答案 1 :(得分:0)

我们可以尝试使用相关子查询进行更新:

UPDATE Intend i
SET slno = (SELECT o.slno FROM "Order" o WHERE i.Oano = o.oano AND i.code = o.code);

请注意,命名表Order(这是SQL中的保留关键字)通常是个坏主意,因为这意味着您每次引用该表名时都必须转义该表名。

答案 2 :(得分:0)

只需使用内部联接

with "Order"(oano, code,slno) as
(
 select 1808,'ln',1 from dual union all
 select 1808,'lk',2 from dual union all
 select 1808,'lc',3 from dual union all
 select 1809,'ki',1 from dual union all 
 select 1809,'dl',2 from dual
), Intend(Intendno, Oano, code) as
(
 select 'I/1',1808,'ln' from dual union all
 select 'I/1',1808,'lc' from dual union all
 select 'I/2',1809,'ki' from dual union all
 select 'I/3',1809,'dl' from dual
)
select i.*, o.slno 
  from Intend i
  join "Order" o
    on o.Oano = i.Oano 
       and o.code = i.code;

 INTENDNO OANO CODE SLNO
 -------- ---- ---- ----
 I/1      1808  ln   1
 I/1      1808  lc   3
 I/2      1809  ki   1
 I/3      1809  dl   2

列出所需的结果。

Demo