Re: INSERT INTO...RETURNING vs SELECT

Lists: pgsql-sql
From: Cliff Wells <cliff(at)develix(dot)com>
To: pgsql-sql(at)postgresql(dot)org
Subject: INSERT INTO...RETURNING vs SELECT
Date: 2010-04-05 04:48:52
Message-ID: 1270442932.4749.140.camel@portable-evil
Views: Raw Message | Whole Thread | Download mbox | Resend email
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


From: Andreas Kretschmer <akretschmer(at)spamfence(dot)net>
To: pgsql-sql(at)postgresql(dot)org
Subject: Re: INSERT INTO...RETURNING vs SELECT
Date: 2010-04-05 07:10:26
Message-ID: 20100405071026.GA5610@tux
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-sql

Cliff Wells <cliff(at)develix(dot)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°


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

On Mon, 2010-04-05 at 09:10 +0200, Andreas Kretschmer wrote:
> Cliff Wells <cliff(at)develix(dot)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