pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Cliff Wells <cliff@develix.com>
To: pgsql-sql@postgresql.org
Subject: INSERT INTO...RETURNING vs SELECT
Date: Sun, 04 Apr 2010 15:12:53 -0700
Message-ID: <1270419173.4749.139.camel@portable-evil> (raw)

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




view thread (4+ messages)  latest in thread

Message-ID: <1270419173.4749.139.camel@portable-evil>
Permalink:  ../1270419173.4749.139.camel@portable-evil/
Also on:    postgresql.org/message-id/1270419173.4749.139.camel@portable-evil

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: cliff@develix.com
  Subject: Re: INSERT INTO...RETURNING vs SELECT
  In-Reply-To: <1270419173.4749.139.camel@portable-evil>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox