Re: Update on Insert

From: "Dann Corbit" <DCorbit(at)connx(dot)com>
To: <sfeher(at)gmail(dot)com>, <pgsql-general(at)postgresql(dot)org>
Subject: Re: Update on Insert
Date: 2009-11-16 22:21:27
Message-ID: D425483C2C5C9F49B5B7A41F89441547029625F7@postal.corporate.connx.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

> -----Original Message-----
> From: pgsql-general-owner(at)postgresql(dot)org [mailto:pgsql-general-
> owner(at)postgresql(dot)org] On Behalf Of Sebastian Feher
> Sent: Monday, November 16, 2009 10:41 AM
> To: pgsql-general(at)postgresql(dot)org
> Subject: [GENERAL] Update on Insert
>
> Hi Everyone,
>
> Given a table "Items" with a PK "item1" and "Qty" - a numeric column
> I'd like to define a way in Postgres to insert when item11 doesn't
> exist already in "Items" and update the Qty by adding the new quantity
> to the existent when the item11 exists. What is a good approach and
> where should I be looking for details. Thanks.

That is called MERGE. Merge is not currently supported:
http://www.postgresql.org/docs/8.4/interactive/unsupported-features-sql-
standard.html

You could set the transaction level to Serializable, start a
transaction, check for existence, if the row exists perform an update,
else perform an insert. Then commit.

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Gauthier, Dave 2009-11-16 22:51:41 specifying a password on the cmd line for db connect
Previous Message manitou-sig 2009-11-16 21:11:47 Re: passing parameters to multiple statements