我正在运行更新查询,如

update datavalue
   set categoryoptioncomboid = '21519'
 where dataelementid = '577' and
       categoryoptioncomboid = '471';

但这是个错误
ERROR:  duplicate key value violates unique constraint "datavalue_pkey"
DETAIL:  Key (dataelementid, periodid, sourceid, categoryoptioncomboid, attributeoptioncomboid)=(577, 35538, 10299, 21519, 15) already exists.

有没有办法让postgres继续更新并跳过任何错误?有没有办法不使用循环的过程?
谢谢

最佳答案

我想试试这样的:

update datavalue
set categoryoptioncomboid = '21519'
where
  dataelementid = '577' and categoryoptioncomboid = '471'
  and not exists (
    select 1
    from datavalue dv
    where dv.dataelementid=datavalue.dataelementid
      and dv.periodid=datavalue.periodid
      and dv.sourceid=datavalue.sourceid
      and dv.categoryoptioncomboid='21519'
      and dv.attributeoptioncomboid=datavalue.attributeoptioncomboid
  );

另一个想法是用insert删除不需要的行。但它需要了解on conflict表列的完整定义。

关于postgresql - 如何忽略更新查询中的错误?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/51493951/

10-11 08:54