Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Plsql 使用两个主键时使用ora-30926_Plsql_Oracle Sqldeveloper - Fatal编程技术网

Plsql 使用两个主键时使用ora-30926

Plsql 使用两个主键时使用ora-30926,plsql,oracle-sqldeveloper,Plsql,Oracle Sqldeveloper,我得到以下错误 ORA-30926:无法在源表中获取一组稳定的行 尝试执行以下语句时: 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_tot

我得到以下错误

ORA-30926:无法在源表中获取一组稳定的行

尝试执行以下语句时:

  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);
我认为这与国家id和评估日期都是我新表格中的主键这一事实有关。我目前在更新声明中对国家id进行了评论,因为这给了我ora-38104

ORA-38104:无法更新ON子句中引用的列: “NT”。“国家/地区ID”

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


编辑:select语句本身运行良好。例如,以下是该查询返回的内容:

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

编辑:如果您有复合主键,则所有主键列都需要在ON子句中,而不是在update中:

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

select语句本身运行良好。例如,有11行ID为:1。第二个键是日期,它为每一行每个id提供一个唯一的日期。是的,这11个id(共1个)是您的副本。现在我知道您有一个复合主键,请参见修改后的示例。这很有效。唯一的区别是我必须在ON语句中使用AND而不是逗号:ON(rec.id=nt.country\u id和rec.evaluation\u date=nt.evaluation\u date)