INSERT INTO...RETURNING vs SELECT - Mailing list pgsql-sql

From Cliff Wells
Subject INSERT INTO...RETURNING vs SELECT
Date
Msg-id 1270442932.4749.140.camel@portable-evil
Whole thread Raw
Responses Re: INSERT INTO...RETURNING vs SELECT
List 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')
RETURNINGid), '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



pgsql-sql by date:

Previous
From: Brad Nicholson
Date:
Subject: Re: Capacity planning.
Next
From: Andreas Kretschmer
Date:
Subject: Re: INSERT INTO...RETURNING vs SELECT