Received: from maia.hub.org (unknown [200.46.204.183]) by mail.postgresql.org (Postfix) with ESMTP id 48419634D3E for ; Mon, 5 Apr 2010 01:49:06 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 30350-02 for ; Mon, 5 Apr 2010 04:48:55 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from mail.develix.com (mail.develix.com [69.168.53.29]) by mail.postgresql.org (Postfix) with ESMTP id C33B8632385 for ; Mon, 5 Apr 2010 01:48:55 -0300 (ADT) Received: from [192.168.1.199] (c-67-189-90-169.hsd1.or.comcast.net [67.189.90.169]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by mail.develix.com (Postfix) with ESMTP id BCC8EC3E04E for ; Sun, 4 Apr 2010 21:48:54 -0700 (PDT) Subject: INSERT INTO...RETURNING vs SELECT From: Cliff Wells To: pgsql-sql@postgresql.org Content-Type: text/plain Date: Sun, 04 Apr 2010 21:48:52 -0700 Message-Id: <1270442932.4749.140.camel@portable-evil> Mime-Version: 1.0 X-Mailer: Evolution 2.26.1 Content-Transfer-Encoding: 7bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0.001 tagged_above=-10 required=5 tests=BAYES_50=0.001 X-Spam-Level: X-Archive-Number: 201004/2 X-Sequence-Number: 33929 I was doing some experimenting and was wondering why the following does not work: CREATE TABLE accounts ( id SERIAL PRIMARY KEY NOT NULL, accounts_id INTEGER REFERENCES accounts, name TEXT ); INSERT INTO accounts (accounts_id, name) VALUES ( (INSERT INTO accounts (accounts_id, name) VALUES (NULL, 'test 1') RETURNING id), 'test 2'); ERROR: syntax error at or near "INTO" Of course, the following works fine: INSERT INTO accounts (accounts_id, name) VALUES (NULL, 'test 1'); INSERT INTO accounts (accounts_id, name) VALUES ( (SELECT id FROM accounts WHERE name='test 1'), 'test 2'); As far as I can see, INSERT INTO...RETURNING is semantically equivalent to SELECT...FROM with a side-effect, so it seems this construct should work. Can someone shed some light? Regards, Cliff