Skip site navigation (1) Skip section navigation (2)

Re: UPDATE FROM syntax

From: Bruce Momjian <pgman(at)candle(dot)pha(dot)pa(dot)us>
To: Paul Makepeace <postgresql(dot)org(at)paulm(dot)com>
Cc: PostgreSQL Docs <pgsql-docs(at)postgresql(dot)org>
Subject: Re: UPDATE FROM syntax
Date: 2004-02-29 01:05:41
Message-ID: (view raw, whole thread or download thread mbox)
Lists: pgsql-docs
Paul Makepeace wrote:
> Hi, is there any documentation for Pg's UPDATE FROM (apparently
> non-standard) syntax? It is mentioned briefly in \h update and not at
> all in
> or
> (directly).

Yea, it is kind of special for UPDATE, so we just have it in the UPDATE
manual page.

> [moving a bit OT for docs?]
> I solved my own problem but was still curious since it's non-standard if
> there's a "more standard" way of updating a table from another query.
>     ALTER TABLE components ADD COLUMN default_body TEXT;
>     UPDATE components SET default_body = bb.body
>     FROM (SELECT b.body, cpb.component_id
>             FROM componentspagesbodies cpb NATURAL JOIN bodies b
>            WHERE cpb.page_id IS NULL) AS bb
>     WHERE bb.component_id = components.component_id;
> (Incidently, I tried UPDATE components c SET ... c.component_id but the
> parser wasn't happy with it.)

No, there really isn't.  The only standard way I know of is to use a
subselect in the SET clause:

	SET val = (SELECT ...)

The big downside of this is the the SELECT is run for every query, and
it can't be used as part of the WHERE clause.  (You would need another
subquery there.)

  Bruce Momjian                        |
  pgman(at)candle(dot)pha(dot)pa(dot)us               |  (610) 359-1001
  +  If your life is a hard drive,     |  13 Roberts Road
  +  Christ can be your backup.        |  Newtown Square, Pennsylvania 19073

In response to

pgsql-docs by date

Next:From: Kris JurkaDate: 2004-03-01 04:48:59
Subject: new PDFs of 7.4 docs
Previous:From: Bruce MomjianDate: 2004-02-27 01:22:28
Subject: plpython doc update

Privacy Policy | About PostgreSQL
Copyright © 1996-2017 The PostgreSQL Global Development Group