insert/update - Mailing list pgsql-sql

From David Welton
Subject insert/update
Date
Msg-id 19981012132325.N31505@gate.cks.com
Whole thread Raw
List pgsql-sql
[ please CC replies to me ]

I've been reading my sql, looking through the postgres docs, but still
haven't managed to find a clean way to do what I want, to wit:

$foo{"bar"} ++ ; # expressed in perl..

if $foo{"bar"} does not exist, it gets created and set to 1.  If it
does exist, it gets increased by one.  In SQL, it seems as if there is
no way to do this in one operation, and without using some sort of
language -

if (foo exists in table.row)
  update table set row2 = row2 + 1 where row = 'foo';
else
  insert into table values ('foo', 1);

This is certainly not elegant, IMHO, and would like to find a way of
doing it in just sql - is this possible?  I would like this to be
portable to other sql implementations, if at all possible.

Thanks,
--
David Welton                          http://www.efn.org/~davidw

    Debian GNU/Linux - www.debian.org

pgsql-sql by date:

Previous
From: Blaise Laurent
Date:
Subject: french caracters in Postgresql database ?
Next
From: Roberto Joao Lopes Garcia
Date:
Subject: Re: [SQL] french caracters in Postgresql database ?