Обсуждение: insert/update

Поиск
Список
Период
Сортировка

insert/update

От
David Welton
Дата:
[ 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