Skip site navigation (1) Skip section navigation (2)

Re: SQL( "if ...exists...),how to do it in the PostgreSQL?

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Peter Eisentraut <peter_e(at)gmx(dot)net>
Cc: Eric <e-lz(at)263(dot)net>, pgsql-hackers(at)postgresql(dot)org
Subject: Re: SQL( "if ...exists...),how to do it in the PostgreSQL?
Date: 2001-06-06 15:58:49
Message-ID: 24541.991843129@sss.pgh.pa.us (view raw or flat)
Thread:
Lists: pgsql-hackers
>> if not exists(select id from test) insert into test(id) values (280);

> Write a function in PL/pgSQL.

That particular case could be handled like so:

insert into test(id) select 280 where not exists(select id from test);

The select produces either zero or one row depending on whether its
WHERE is true.  Voila, problem solved.  It's even nearly standard ;-)
although in something like Oracle you'd have to add "from dual", I
think.

			regards, tom lane

In response to

pgsql-hackers by date

Next:From: rai lalitDate: 2001-06-06 16:03:59
Subject: Delete oid causing problems
Previous:From: Martín MarquésDate: 2001-06-06 15:57:13
Subject: Re: [HACKERS] something smells bad

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group