在Oracle SQL Developer中更新PL / SQL中的多行时忽略重复项

时间:2016-09-14 17:39:29

标签: sql oracle11g sql-update

当与另一个表(B)匹配时,我正在尝试更新表(A)的所有行。问题是我收到以下错误:

无法在源表中获得稳定的行集

我已经进行了研究,我知道原因可能是其中一个表中的重复行。只有表B有重复的行,我试过用一些查询来忽略它们,但是没有成功。

 merge into A x 
    using B y
    on (x.id= y.id)
    when matched then
      UPDATE SET 
      x.apples= y.apples,
      x.bananas= y.bananas,
      x.grapes= y.grapes;

有人可以帮忙吗?

提前致谢

2 个答案:

答案 0 :(得分:0)

重复项可以位于您进行更新的一侧。 但你不能在源头上有副本。
想想看,他们的sql引擎不知道在更新中使用了多个记录中的哪一个。您需要修复重复的问题。或者使用某种最大值或最小值来获得要在更新中使用的唯一数据集。

答案 1 :(得分:0)

我能够解决这个问题,这就是我所做的解决方案:

 merge into A x 
        using (select distinct id from B) y
        on (x.id= y.id)
        when matched then
          UPDATE SET 
          x.apples= (select apples from B where id = 
(select distinct id from B where id = x.id) and rownum = '1'),
          x.bananas= (select bananas from B where id = 
(select distinct id from B where id = x.id) and rownum = '1'),
          x.grapes= (select grapes from B where id = 
(select distinct id from B where id = x.id ) and rownum = '1');