Re: Combining INSERT with DELETE RETURNING

From: "David G(dot) Johnston" <david(dot)g(dot)johnston(at)gmail(dot)com>
To: Alexander Farber <alexander(dot)farber(at)gmail(dot)com>
Cc: pgsql-general <pgsql-general(at)postgresql(dot)org>
Subject: Re: Combining INSERT with DELETE RETURNING
Date: 2017-03-24 15:19:33
Message-ID: CAKFQuwbJThWgzEO6hUNFwgbctSn8kvS+y-J74yjP2gntVbk8+Q@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Fri, Mar 24, 2017 at 8:06 AM, Alexander Farber <
alexander(dot)farber(at)gmail(dot)com> wrote:

> Good afternoon,
>
> the doc https://www.postgresql.org/docs/9.6/static/sql-delete.html
> states:
>
> "The syntax of the RETURNING list is identical to that of the output list
> of SELECT."
> ​[...]​
>
> words=> \i words_merge_users.sql
> psql:words_merge_users.sql:218: ERROR: syntax error at or near "FROM"
> LINE 131: DELETE FROM words_reviews
>

​That doesn't work for SELECT either...

INSERT INTO tbl_trg (id)
VALUES (
SELECT 1
);

ERROR: syntax error at or near "SELECT"

This should help with the SELECT variation:

https://www.postgresql.org/docs/9.5/static/sql-insert.html

That said the page does say:

"query
A query (SELECT statement) that supplies the rows to be inserted. Refer to
the SELECT statement for a description of the syntax.
"

So directly replace the actual SELECT query with a DELETE-RETURNING doesn't
seem to work. You will need to perform the DELETE separately and then
funnel those records through a SELECT statement. A subquery may work
though a CTE is likely considered best practice.

David J.

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Thomas Kellerer 2017-03-24 15:30:35 Re: Combining INSERT with DELETE RETURNING
Previous Message Alexander Farber 2017-03-24 15:06:39 Combining INSERT with DELETE RETURNING