【问题标题】:Ignoring duplicates when updating multiple rows in PL/SQL in Oracle SQL Developer在 Oracle SQL Developer 中更新 PL/SQL 中的多行时忽略重复项
【发布时间】:2017-01-22 14:54:03
【问题描述】:

当 id 与另一个表 (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;

有人可以帮忙吗?

提前致谢

【问题讨论】:

  • 您的问题中没有 PL/SQL。

标签: sql oracle11g sql-update


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    我能够解决这个问题,这是我提出的解决方案:

     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');
    

    【讨论】:

      猜你喜欢
      • 2021-09-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-30
      • 1970-01-01
      • 1970-01-01
      • 2015-12-03
      • 2015-09-16
      相关资源
      最近更新 更多