Re: Use select and update together - Mailing list pgsql-sql

From Samuel Gendler
Subject Re: Use select and update together
Date
Msg-id CAEV0TzDkrz9uOisHXPKKfXMaeMaPwedCtisxoqtyGn66Q97iDg@mail.gmail.com
Whole thread Raw
In response to Use select and update together  (Boris <kuzikoff@ukr.net>)
List pgsql-sql


On Mon, Sep 12, 2011 at 3:36 AM, Boris <kuzikoff@ukr.net> wrote:

SELECT (
        UPDATE tbl SET val = 1
            WHERE KEY = any('{0,1,2,3,4,5}'::int[])
        returning key
);

cause syntax error. Is any query of such type (update warped into
select) is possible?


Just lose the select (); part.

update tbl set val = 1 where condition = true returning *;

That works just fine on 8.4.  You can also specify individual columns in the returning clause.  The postgresql documentation covers the topic.


It doesn't appear to work in a subquery, so you can't join to it (though you can use a join within the update clause and then return the columns you need from a joined table, I suspect).  Simply returning 1 or more columns from updated rows works just fine, though.

pgsql-sql by date:

Previous
From: pasman pasmański
Date:
Subject: Re: Use select and update together
Next
From: Guillaume Roger
Date:
Subject: Partition over a sliding date window