Re: Testing for existence of a record before insert

Поиск
Список
Период
Сортировка
От Bruno Wolff III
Тема Re: Testing for existence of a record before insert
Дата
Msg-id 20040323175943.GA26654@wolff.to
обсуждение исходный текст
Ответ на Testing for existence of a record before insert  ("Rob Anderson" <roba@bml.uk.com>)
Список pgsql-admin
On Tue, Mar 23, 2004 at 16:41:40 -0000,
  Rob Anderson <roba@bml.uk.com> wrote:
> I am trying to find a way of performing something similar to the SQL-Server SQL of
>
> IF NOT EXISTS (SELECT id FROM a_table WHERE id='akey')
> THEN INSERT INTO a_table (id, name) values ('akey', 'adetails');

INSERT INTO a_table (id, name)
  SELECT 'akey' as id, 'adetails' as name where not exists
    (SELECT id FROM a_table WHERE id='akey')

It is still possible for two transactions to both try to insert the
same value at the same time. You either need to be prepared to handle
the occasional error or you need to get an exclusive lock on a_table.

В списке pgsql-admin по дате отправления:

Предыдущее
От: "Rob Anderson"
Дата:
Сообщение: Testing for existence of a record before insert
Следующее
От: Bruce Momjian
Дата:
Сообщение: Re: limiting \l [list all databases]