pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Stefan Becker <stefan@yukonho.de>
To: pgsql-sql@postgresql.org
Subject: Is there a solution for "SELECT OR INSERT"
Date: Mon, 29 Nov 2010 00:30:47 +0100
Message-ID: <000001cb8f54$4a9e3c20$dfdab460$@de> (raw)

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






view thread (2+ messages)  latest in thread

Message-ID: <000001cb8f54$4a9e3c20$dfdab460$@de>
Permalink:  ../000001cb8f54$4a9e3c20$dfdab460$@de/
Also on:    postgresql.org/message-id/000001cb8f54$4a9e3c20$dfdab460$@de

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: stefan@yukonho.de
  Subject: Re: Is there a solution for "SELECT OR INSERT"
  In-Reply-To: <000001cb8f54$4a9e3c20$dfdab460$@de>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

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