how to do merge in postgres ("with upsert as" not supported) - Mailing list pgsql-general

From Tong Michael
Subject how to do merge in postgres ("with upsert as" not supported)
Date
Msg-id CAHVHtZ-HiRbVEO73bFQLQKC1-oWyg1TFK_U5MoY_3x5Q=2to5A@mail.gmail.com
Whole thread Raw
Responses Re: how to do merge in postgres ("with upsert as" not supported)  (Jim Nasby <Jim.Nasby@BlueTreble.com>)
Re: how to do merge in postgres ("with upsert as" not supported)  (Thomas Kellerer <spam_eater@gmx.net>)
List pgsql-general

hey, guys, I came across a merge statement when I'm trying to convert stored procedures from Mysql to Postgres:

 

merge into db.ChargePeriod d

using (

    select ba.ClientID

         , ba.BillingAccountID

         , bs.BillingScheduleID

         , @CodeWithholdD as WithholdTypeID

      from db.ClientPartyIDConfiguration cpc

      join db.BillingAccount ba

        on ba.Deleted = 0

       and ba.ClientID = cpc.ClientID

       and ba.PartyID = cpc.PartyID

       and convert(date,getdate()) between ba.EffectiveDate and ba.ExpireDate

      join db.BillingSchedule bs

        on bs.Deleted = 0

       and bs.ClientID = ba.ClientID

       and bs.CoverageBeginDate >= ba.EffectiveDate

       and bs.CoverageBeginDate <= ba.ExpireDate

    where cpc.Deleted = 0

       and cpc.ClientID = @ClientID

) s on d.Deleted = 0

   and d.ClientID = s.ClientID

   and d.BillingAccountID = s.BillingAccountID

   and d.BillingScheduleID = s.BillingScheduleID

  when matched

   and isNull(d.WithholdTypeID,-1) <> isNull(s.WithholdTypeID,-1)

  then update

   set WithholdTypeID = s.WithholdTypeID

     , UpdateUser     = @UpdateUser

     , UpdateDate     = @UpdateDate

  when not matched then insert (

           ClientID

         , BillingAccountID

         , BillingScheduleID

         , WithholdTypeID

         , CreateUser

         , CreateDate

         , Deleted

         , CancelDate

       ) values (

           s.ClientID

         , s.BillingAccountID

         , s.BillingScheduleID

         , s.WithholdTypeID

         , @UpdateUser

         , @UpdateDate

         , 0

         , '9999-12-31'

       )

;

 

I saw some people use "with upsert as", but my pgAdmin version(1.8) doesn't support it. Anyone has any ideas how to do merge in postgres?

 

Thanks.


pgsql-general by date:

Previous
From: John Turner
Date:
Subject: Re: range type expression syntax
Next
From: Merlin Moncure
Date:
Subject: Re: [HACKERS] Composite index and min()