在PostgreSQL 12.2中合并

时间:2020-05-02 16:15:30

标签: postgresql

postgresql不支持合并,相反,我们必须使用upsert,在postgre中发生冲突时插入。 我在oracle中有以下合并。我知道哪种方法在postgres中是正确的。

MERGE INTO itm m
      USING(SELECT rec.xncPRCNEW   AS IMPPRCNEW,
                    rec.xncCSSTDT   AS IMPCSSTDT,
                   ) h
      ON ((m.IMPITMID = loc_IMPVDRITMID or m.IMPVDRITMID = loc_IMPVDRITMID) 
      AND m.IMPVDRID = loc_VDRID AND (coalesce(m.IMPSITE, 0) = coalesce(loc_IMPSITE, 0) or coalesce(m.IMPZONE, 0) = coalesce(loc_IMPSITE, 0)) and m.IMPSTDT = TO_DATE(rec.xncCSSTDT, 'MM/DD/YYYY') and m.IMPEDDT = TO_DATE(rec.xncCSENDT, 'MM/DD/YYYY'))
      WHEN MATCHED THEN
        UPDATE
           SET m.IMPCHG    = h.IMPPRCNEW,
               m.IMPSTS    = CASE WHEN CASE WHEN loc_autoapp=1 THEN  'VP'  ELSE 'SB' END ='SB' THEN (CASE WHEN loc_count=0 THEN  'SB'  ELSE 'VP' END )  ELSE CASE WHEN loc_autoapp=1 THEN  'VP'  ELSE 'SB' END  END ,
      WHEN NOT MATCHED THEN
        INSERT(IMPID,
           IMPITMID,
           IMPLVID,
           IMPITMDESC,
           IMPVDRCCNO)
        VALUES
          (nextval('mass_purchaseprice_sqn'),
           trim(both substr(rec.xncitmid,
                       INSTR(rec.xncitmid, ']', 1, 3) + 1,
                       300)),
           CASE WHEN CASE WHEN loc_autoapp=1 THEN  'VP'  ELSE 'SB' END ='SB' THEN (CASE WHEN loc_count=0 THEN  'SB'  ELSE 'VP' END )  ELSE CASE WHEN loc_autoapp=1 THEN  'VP'  ELSE 'SB' END  END ,
           (select vdrccno
              from vdrgen
             where vdrextno =
                   substr(rec.xncvdrid,
                          1,
                          position(' - ' in rec.xncvdrid) - 1)));

      RAISE NOTICE 'test3';
    END;

在这种情况下,当匹配和不匹配时,我们都有两个。

0 个答案:

没有答案