Re: Use select and update together

From: Samuel Gendler <sgendler(at)ideasculptor(dot)com>
To: Boris <kuzikoff(at)ukr(dot)net>
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: Use select and update together
Date: 2011-09-13 06:45:11
Message-ID: CAEV0TzDkrz9uOisHXPKKfXMaeMaPwedCtisxoqtyGn66Q97iDg@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

On Mon, Sep 12, 2011 at 3:36 AM, Boris <kuzikoff(at)ukr(dot)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.

http://www.postgresql.org/docs/9.0/static/sql-update.html

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.

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Guillaume Roger 2011-09-13 11:25:39 Partition over a sliding date window
Previous Message pasman pasmański 2011-09-13 05:50:10 Re: Use select and update together