pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Is there a solution for "SELECT OR INSERT"
2+ messages / 2 participants
[nested] [flat]

* Is there a solution for "SELECT OR INSERT"
@ 2010-11-28 23:30  Stefan Becker <stefan@yukonho.de>
  0 siblings, 1 reply; 2+ messages in thread

From: Stefan Becker @ 2010-11-28 23:30 UTC (permalink / raw)
  To: pgsql-sql

given a simply table table like this:

create table MyTabl 
(id serial,
 lname varchar,
 primary key (id)
)

I am trying to return the Primary-Key for a given
row regardless of the wanted row already being 
in the table.

using something like:

select coalesce 
(
  (select id from MyTabl where lname='wanted'),
  (insert into MyTabl (lname) values ('wanted') returning id)
) as id;

the above doesn't allow the insert..returning even thow
a similar select statement would be okay.
Is there a way to get the ID row OR create a new one in
ONE single statement?

my best regards and thank-you 
for your time....

Stefan






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

* Re: Is there a solution for "SELECT OR INSERT"
@ 2010-11-30 14:16  Florian Weimer <fweimer@bfk.de>
  parent: Stefan Becker <stefan@yukonho.de>
  0 siblings, 0 replies; 2+ messages in thread

From: Florian Weimer @ 2010-11-30 14:16 UTC (permalink / raw)
  To: pgsql@yukonho.de; +Cc: pgsql-sql

* Stefan Becker:

> Is there a way to get the ID row OR create a new one in
> ONE single statement?

You could create a stored procedure.  But if you have concurrent
inserts, locking is a bit tricky.

-- 
Florian Weimer                <fweimer@bfk.de>
BFK edv-consulting GmbH       http://www.bfk.de/
Kriegsstraße 100              tel: +49-721-96201-1
D-76133 Karlsruhe             fax: +49-721-96201-99



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


end of thread, other threads:[~2010-11-30 14:16 UTC | newest]

Thread overview: 2+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2010-11-28 23:30 Is there a solution for "SELECT OR INSERT" Stefan Becker <stefan@yukonho.de>
2010-11-30 14:16 ` Florian Weimer <fweimer@bfk.de>

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