我们正在将Oracle迁移到Postgresql 12.2。我们知道Postgresql中没有使用Merge。相反,我们必须使用upsert,在冲突中插入。但我们收到语法错误。这是代码。
MERGE INTO itmmasprc m
USING(SELECT rec.xncPRCNEW AS IMPPRCNEW,
rec.xncUPDDT AS IMPUPDDT,
rec.xncCMT AS IMPCMT,
rec.xncNEWSTRDT AS IMPNEWSTRDT,
rec.xncNEWENDDT AS IMPNEWENDDT
) 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.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 ,
m.IMPCSENDT = to_date(h.IMPNEWENDDT, 'MM/DD/YYYY'),
WHEN NOT MATCHED THEN
INSERT(IMPID,
IMPSTS,
IMPDPTID,
IMPCREDT,
IMPUPDBY,
IMPUPDDT,
IMPVDRCCNO)
VALUES
(nextval('mass_purchaseprice_sqn'),
trim(both substr(rec.xncitmid,
INSTR(rec.xncitmid, ']', 1, 3) + 1,
300)),
round((rec.xncPRCNEW)::numeric, 2),
substr(rec.xncbuyrid,
INSTR(rec.xncbuyrid, ' ', 1, 1) + 1,
(INSTR(rec.xncbuyrid, '-', 1, 1) -
(INSTR(rec.xncbuyrid, ' ', 1, 1)) - 2)),
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 ,
PK_VP_COSTUPLOAD.f_get_dept(loc_IMPVITMID,
rec.xncUPC,
loc_VDRID),
(select vdrccno
from vdrgen
where vdrextno =
substr(rec.xncvdrid,
1,
position(' - ' in rec.xncvdrid) - 1)));
RAISE NOTICE 'test3';