INSERT INTO...RETURNING vs SELECT

From: Cliff Wells <cliff(at)develix(dot)com>
To: pgsql-sql(at)postgresql(dot)org
Subject: INSERT INTO...RETURNING vs SELECT
Date: 2010-04-04 22:12:53
Message-ID: 1270419173.4749.139.camel@portable-evil
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

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

Browse pgsql-sql by date

  From Date Subject
Next Message Cliff Wells 2010-04-05 04:48:52 INSERT INTO...RETURNING vs SELECT
Previous Message junaidmalik14 2010-04-03 12:58:53 count function alternative in postgres