Need to convert Oracle “merge into” query to PostgreSQL

限于喜欢 提交于 2020-01-25 06:53:45

问题


I'm trying to convert the following Oracle query to PostgreSQL:

  MERGE into feepay.TRPT_W2_REPORTS TRPT1 
  USING(
  WITH RWS AS
    (SELECT PROG.BINCLIENT, TRPT.PUT_DIRECTORY
    FROM  feepay.program2 PROG
    INNER JOIN  feepay.TRPT_W2_PROGRAMS TRPT
    ON (PROG.BINCLIENT = TRPT.BINCLIENT OR PROG.ISSUER_ID = TRPT.ISSUER_ID))
  SELECT TCI.CUSTOMERNAME AS ACCOUNT,
  TC.CUSTOMER_ID AS urn,
  TC.LAST_NAME,
  TC.FIRST_NAME,
  TC.DOB,
  TCA.ADDRESS

  FROM feepay.TAU_CARDNUMBERS TCN
  INNER JOIN feepay.TAU_CUSTOMER_CARDNUMBER TCCN ON (TCN.CARDNUMBER_ID = TCCN.CARDNUMBER_ID)
  INNER JOIN feepay.TBLCUSTOMERS TC ON (TCCN.CUSTOMER_ID = TC.CUSTOMER_ID)
  LEFT JOIN feepay.tau_customeraddress TCA ON (TC.CUSTOMER_ID = TCA.CUSTOMER_ID)
  INNER JOIN feepay.TAU_ISSUER TI ON (TI.ISSUER_ID = TCN.ISSUER_ID)
  INNER JOIN feepay.TBLCUSTOMERS TCI ON (TCI.CUSTOMER_ID = TI.CUSTOMER_ID)
  LEFT JOIN feepay.TRPT_W2_REPORTS TRPT ON (TRPT.URN = TC.CUSTOMER_ID)
  WHERE BINCLIENT IN (SELECT BINCLIENT FROM RWS)
  AND TC.CUSTOMERNAME NOT IN ('freepay card','svds card')) TRPT2
  ON (TRPT1.URN = TRPT2.URN)
  WHEN MATCHED THEN
  UPDATE SET
  TRPT1.ACCOUNT = TRPT2.ACCOUNT,
  TRPT1.LAST_NAME = TRPT2.LAST_NAME,
  TRPT1.FIRST_NAME = TRPT2.FIRST_NAME,
  TRPT1.DOB = TRPT2.DOB,
  TRPT1.ADDRESS = TRPT2.ADDRESS,
  TRPT1.LAST_UPDATE = now(),
  TRPT1.STATUS = 'u' /* uPDATED */
  WHEN NOT MATCHED THEN
  INSERT (ACCOUNT, URN, LAST_NAME, FIRST_NAME, ISENTITY, DOB, ADDRESS, LAST_UPDATE, STATUS)
  VALUES (TRPT2.ACCOUNT, TRPT2.URN, TRPT2.LAST_NAME, TRPT2.FIRST_NAME, 'y', TRPT2.DOB, TRPT2.MIDDLE_NAME, 
    TRPT2.ADDRESS, now(), 'i');

Unfortunately PostgreSQL does not support MERGE, so I'm really stuck. I hope some database pro out of here can help me with this.


回答1:


You can use INSERT ON CONFLICT () instead:

insert into feepay.TRPT_W2_REPORTS (ACCOUNT, URN, LAST_NAME, FIRST_NAME, ISENTITY, DOB, ADDRESS, LAST_UPDATE, STATUS)
WITH RWS AS (
  SELECT PROG.BINCLIENT, TRPT.PUT_DIRECTORY
  FROM  feepay.program2 PROG
    INNER JOIN  feepay.TRPT_W2_PROGRAMS TRPT
            ON (PROG.BINCLIENT = TRPT.BINCLIENT OR PROG.ISSUER_ID = TRPT.ISSUER_ID)
)
SELECT TCI.CUSTOMERNAME,
       TC.CUSTOMER_ID,
       TC.LAST_NAME,
       TC.FIRST_NAME,
       'Y'
       TC.DOB,
       TCA.ADDRESS, 
       now(), 
       'i'
FROM feepay.TAU_CARDNUMBERS TCN
  INNER JOIN feepay.TAU_CUSTOMER_CARDNUMBER TCCN ON (TCN.CARDNUMBER_ID = TCCN.CARDNUMBER_ID)
  INNER JOIN feepay.TBLCUSTOMERS TC ON (TCCN.CUSTOMER_ID = TC.CUSTOMER_ID)
  LEFT JOIN feepay.tau_customeraddress TCA ON (TC.CUSTOMER_ID = TCA.CUSTOMER_ID)
  INNER JOIN feepay.TAU_ISSUER TI ON (TI.ISSUER_ID = TCN.ISSUER_ID)
  INNER JOIN feepay.TBLCUSTOMERS TCI ON (TCI.CUSTOMER_ID = TI.CUSTOMER_ID)
  LEFT JOIN feepay.TRPT_W2_REPORTS TRPT ON (TRPT.URN = TC.CUSTOMER_ID)
WHERE BINCLIENT IN (SELECT BINCLIENT FROM RWS)
  AND TC.CUSTOMERNAME NOT IN ('freepay card','svds card')) TRPT2
ON CONFLICT (URN)
DO UPDATE SET
  ACCOUNT = excluded.ACCOUNT,
  LAST_NAME = excluded.LAST_NAME,
  FIRST_NAME = excluded.FIRST_NAME,
  DOB = excluded.DOB,
  ADDRESS = excluded.ADDRESS,
  LAST_UPDATE = now(),
  STATUS = 'u' /* uPDATED */

You need to verify if the columns in the SELECT list match the columns as listed in the INSERT column list.



来源:https://stackoverflow.com/questions/58862839/need-to-convert-oracle-merge-into-query-to-postgresql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!