使用两个主键时出现 ora-30926

ora-30926 when using two primary keys

我收到以下错误

ORA-30926: unable to get a stable set of rows in the source tables

尝试执行以下语句时:

  MERGE INTO new_table nt
    USING ( select c.id, 
                   cd.evaluation_date, cd.population_total_count, cd.population_urban_count, cd.population_birth_rate_per1k,
                   cf.gdp_total_dollars, cf.gdp_per_capita_dollars
            from countries c, country_demographics cd, country_financials cf 
            where c.id = cd.country_id
            and   cd.evaluation_date = cf.evaluation_date
            and   cd.country_id = cf.country_id 
            order by c.id ) rec
    ON ( rec.id = nt.country_id )
    WHEN MATCHED THEN 
      UPDATE SET --nt.country_id             = rec.id,
                 nt.evaluation_date        = rec.evaluation_date,
                 nt.population_total_count = rec.population_total_count,
                 nt.population_urban_count = rec.population_urban_count,
                 nt.population_birth_rate_per1k  = rec.population_birth_rate_per1k,
                 nt.gdp_total_dollars      = rec.gdp_total_dollars,
                 nt.gdp_per_capita_dollars = rec.gdp_per_capita_dollars
    WHEN NOT MATCHED THEN 
      INSERT (country_id, evaluation_date, population_total_count, population_urban_count, population_birth_rate_per1k,
                  gdp_total_dollars, gdp_per_capita_dollars)
      VALUES (rec.id, rec.evaluation_date, rec.population_total_count, rec.population_urban_count, rec.population_birth_rate_per1k,
                  rec.gdp_total_dollars, rec.gdp_per_capita_dollars);

我认为这与 country_id 和 evaluation_date 都是我的 new_table 中的主键有关。我目前在更新语句中评论了 country_id,因为那给了我 ora-38104.

ORA-38104: Columns referenced in the ON Clause cannot be updated: "NT"."COUNTRY_ID"

知道在这种情况下如何处理双键吗?也就是说,如果那是错误的。

编辑:select 语句本身运行良好。例如,这是查询 returns 的内容:

我怀疑 USING 子句中的 SELECT 在 id 列(在 ON 子句中使用)中返回重复值。 运行 它本身并在 id 列上排序以找出答案。 ON 子句中的列是键值,根据定义不能更新并且必须是唯一的。

编辑:如果你有一个复合主键,那么所有主键列都需要在 ON 子句中而不是在更新中:

MERGE INTO new_table nt
    USING ( select c.id, 
                   cd.evaluation_date, cd.population_total_count, cd.population_urban_count, cd.population_birth_rate_per1k,
                   cf.gdp_total_dollars, cf.gdp_per_capita_dollars
            from countries c, country_demographics cd, country_financials cf 
            where c.id = cd.country_id
            and   cd.evaluation_date = cf.evaluation_date
            and   cd.country_id = cf.country_id 
            order by c.id ) rec
    ON ( rec.id = nt.country_id AND rec.evaluation_date = nt.evaluation_date )
    WHEN MATCHED THEN 
      UPDATE SET nt.population_total_count = rec.population_total_count,
                 nt.population_urban_count = rec.population_urban_count,
                 nt.population_birth_rate_per1k  = rec.population_birth_rate_per1k,
                 nt.gdp_total_dollars      = rec.gdp_total_dollars,
                 nt.gdp_per_capita_dollars = rec.gdp_per_capita_dollars
    WHEN NOT MATCHED THEN 
      INSERT (country_id, evaluation_date, population_total_count, population_urban_count, population_birth_rate_per1k,
                  gdp_total_dollars, gdp_per_capita_dollars)
      VALUES (rec.id, rec.evaluation_date, rec.population_total_count, rec.population_urban_count, rec.population_birth_rate_per1k,
                  rec.gdp_total_dollars, rec.gdp_per_capita_dollars);