Received: from maia.hub.org (unknown [200.46.204.183]) by mail.postgresql.org (Postfix) with ESMTP id 2FC9F632798 for ; Sun, 4 Apr 2010 19:31:58 -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 57829-01 for ; Sun, 4 Apr 2010 22:31:47 +0000 (UTC) X-Greylist: delayed 00:18:51.391694 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 BC0666325DF for ; Sun, 4 Apr 2010 19:31:47 -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 11940C3E04E for ; Sun, 4 Apr 2010 15:12: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 15:12:53 -0700 Message-Id: <1270419173.4749.139.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/6 X-Sequence-Number: 33933 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