pg.ddx.io  pgsql-admin@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Testing for existence of a record before insert
2+ messages / 2 participants
[nested] [flat]

* Testing for existence of a record before insert
@ 2004-03-23 16:41  Rob Anderson <roba@bml.uk.com>
  0 siblings, 1 reply; 2+ messages in thread

From: Rob Anderson @ 2004-03-23 16:41 UTC (permalink / raw)
  To: pgsql-admin

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');


---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.619 / Virus Database: 398 - Release Date: 10/03/2004

^ permalink  raw  reply  [nested|flat] 2+ messages in thread

* Re: Testing for existence of a record before insert
@ 2004-03-23 17:59  Bruno Wolff III <bruno@wolff.to>
  parent: Rob Anderson <roba@bml.uk.com>
  0 siblings, 0 replies; 2+ messages in thread

From: Bruno Wolff III @ 2004-03-23 17:59 UTC (permalink / raw)
  To: Rob Anderson <roba@bml.uk.com>; +Cc: 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.



^ permalink  raw  reply  [nested|flat] 2+ messages in thread


end of thread, other threads:[~2004-03-23 17:59 UTC | newest]

Thread overview: 2+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2004-03-23 16:41 Testing for existence of a record before insert Rob Anderson <roba@bml.uk.com>
2004-03-23 17:59 ` Bruno Wolff III <bruno@wolff.to>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox