简体   繁体   English

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

[英]Ignoring duplicates when updating multiple rows in PL/SQL in Oracle SQL Developer

I'm trying to update all the rows of a table (A) when the ids with another table (B) match. 我正在尝试更新与另一个表(B)的ID匹配的表(A)的所有行。 The problem is that i'm getting the following error : 问题是我遇到以下错误:

unable to get a stable set of rows in the source tables 无法在源表中获得稳定的行集

I've made my research and I know that the cause may be duplicated rows in one of the tables. 我已经进行了研究,并且知道原因可能是其中一张表中的行重复。 Only table B has duplicated rows, i've tried in may ways to ignore them with some queries, but unsuccessfully. 只有表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;

Can someone help? 有人可以帮忙吗?

Thanks in advance 提前致谢

Duplicates can be on the side where you are doing the updates. 重复项可以在进行更新的一侧。 But you can not have a duplicate on the source side. 但是在源端不能有重复项。
Think about it they sql engine does not know which of the multiple records to use on the update. 考虑一下,他们sql引擎不知道在更新中使用多个记录中的哪个。 You need to either fix the duplicate issue. 您需要解决重复的问题。 Or do some sort of max or min to get a unique data set to be used in the update. 或者执行某种最大值或最小值来获取要在更新中使用的唯一数据集。

I was able to solve this problem, here's the solution i made: 我能够解决此问题,这是我所做的解决方案:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM