Received: from maia.hub.org (maia-3.hub.org [200.46.204.243]) by mail.postgresql.org (Postfix) with ESMTP id 171BB1337B54 for ; Sun, 28 Nov 2010 21:00:15 -0400 (AST) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.243]) (amavisd-maia, port 10024) with ESMTP id 91368-01 for ; Mon, 29 Nov 2010 01:00:02 +0000 (UTC) X-Greylist: delayed 01:29:05.798146 by SQLgrey-1.7.6 Received: from 208091.vserver.de (nirs.de [62.75.208.91]) by mail.postgresql.org (Postfix) with ESMTP id 9CEEB1335B36 for ; Sun, 28 Nov 2010 21:00:00 -0400 (AST) Received: from vista (p57A59EDA.dip0.t-ipconnect.de [87.165.158.218]) by 208091.vserver.de (8.12.8/8.12.8) with ESMTP id oASNUotn013505 for ; Mon, 29 Nov 2010 00:30:51 +0100 Reply-To: From: "Stefan Becker" To: Subject: Is there a solution for "SELECT OR INSERT" Date: Mon, 29 Nov 2010 00:30:47 +0100 Message-ID: <000001cb8f54$4a9e3c20$dfdab460$@de> MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit X-Mailer: Microsoft Office Outlook 12.0 Thread-Index: AcuPVEjo7I+sK15ST+KMWXmtY1CPpA== Content-Language: de X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-0.001 tagged_above=-5 required=5 tests=BAYES_20=-0.001 X-Spam-Level: X-Archive-Number: 201011/80 X-Sequence-Number: 34818 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