pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
INSERT INTO...RETURNING vs SELECT
4+ messages / 2 participants
[nested] [flat]

* INSERT INTO...RETURNING vs SELECT
@ 2010-04-04 22:12 Cliff Wells <cliff@develix.com>
  0 siblings, 0 replies; 4+ messages in thread

From: Cliff Wells @ 2010-04-04 22:12 UTC (permalink / raw)
  To: 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




^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* INSERT INTO...RETURNING vs SELECT
@ 2010-04-05 04:48 Cliff Wells <cliff@develix.com>
  2010-04-05 07:10 ` Re: INSERT INTO...RETURNING vs SELECT Andreas Kretschmer <akretschmer@spamfence.net>
  0 siblings, 1 reply; 4+ messages in thread

From: Cliff Wells @ 2010-04-05 04:48 UTC (permalink / raw)
  To: 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




^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: INSERT INTO...RETURNING vs SELECT
  2010-04-05 04:48 INSERT INTO...RETURNING vs SELECT Cliff Wells <cliff@develix.com>
@ 2010-04-05 07:10 ` Andreas Kretschmer <akretschmer@spamfence.net>
  2010-04-05 15:40   ` Re: INSERT INTO...RETURNING vs SELECT Cliff Wells <cliff@develix.com>
  0 siblings, 1 reply; 4+ messages in thread

From: Andreas Kretschmer @ 2010-04-05 07:10 UTC (permalink / raw)
  To: pgsql-sql

Cliff Wells <cliff@develix.com> wrote:

> 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?

Well, at the moment you can't reuse the RETURNING-values, you have to
wait for 9.1, writeable CTE.


Andreas
-- 
Really, I'm not out to destroy Microsoft. That will just be a completely
unintentional side effect.                              (Linus Torvalds)
"If I was god, I would recompile penguin with --enable-fly."   (unknown)
Kaufbach, Saxony, Germany, Europe.              N 51.05082°, E 13.56889°



^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: INSERT INTO...RETURNING vs SELECT
  2010-04-05 04:48 INSERT INTO...RETURNING vs SELECT Cliff Wells <cliff@develix.com>
  2010-04-05 07:10 ` Re: INSERT INTO...RETURNING vs SELECT Andreas Kretschmer <akretschmer@spamfence.net>
@ 2010-04-05 15:40   ` Cliff Wells <cliff@develix.com>
  0 siblings, 0 replies; 4+ messages in thread

From: Cliff Wells @ 2010-04-05 15:40 UTC (permalink / raw)
  To: pgsql-sql

On Mon, 2010-04-05 at 09:10 +0200, Andreas Kretschmer wrote:
> Cliff Wells <cliff@develix.com> wrote:
> 
> > 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?
> 
> Well, at the moment you can't reuse the RETURNING-values, you have to
> wait for 9.1, writeable CTE.

Thanks, I just wanted to make sure I wasn't misunderstanding something.

Regards,
Cliff




^ permalink  raw  reply  [nested|flat] 4+ messages in thread


end of thread, other threads:[~2010-04-05 15:40 UTC | newest]

Thread overview: 4+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2010-04-04 22:12 INSERT INTO...RETURNING vs SELECT Cliff Wells <cliff@develix.com>
2010-04-05 04:48 INSERT INTO...RETURNING vs SELECT Cliff Wells <cliff@develix.com>
2010-04-05 07:10 ` Andreas Kretschmer <akretschmer@spamfence.net>
2010-04-05 15:40   ` Cliff Wells <cliff@develix.com>

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