Re: INSERT OR UPDATE - Mailing list pgsql-general

From Guy Rouillier
Subject Re: INSERT OR UPDATE
Date
Msg-id CC1CF380F4D70844B01D45982E671B239E8DD9@mtxexch01.add0.masergy.com
Whole thread Raw
In response to INSERT OR UPDATE  ("Julian Scarfe" <julian@avbrief.com>)
List pgsql-general
Julian Scarfe wrote:
> I'm keeping config information for an application in a series of
> related tables.  I'd like a command that INSERTs data if it's new,
< or UPDATEs it if the key is duplicated.

Write a stored procedure called something like InsertUpdateConfigData.
Pick the operation that you think is most likely to occur more often
(the insert or update).  Code that as the initial statement.  Then
create an exception block, and in that exception block catch the error
that would result from the initial statement being executed in the case
where the other one should have been; then execute that other statement.

There is no magic database-provided SQL statement that says "try an
insert and if that fails then try an update."  You have to do that
yourself.

--
Guy Rouillier


pgsql-general by date:

Previous
From: Tyler MacDonald
Date:
Subject: Re: inserting many rows
Next
From: Lucky Leavell
Date:
Subject: Ident authentication failed