Re: UPDATE of several columns using SELECT statement

From: Sam Mason <sam(at)samason(dot)me(dot)uk>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: UPDATE of several columns using SELECT statement
Date: 2009-03-17 15:25:22
Message-ID: 20090317152522.GB32672@frubble.xen.chris-lamb.co.uk
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Tue, Mar 17, 2009 at 03:55:47PM +0100, adam.slachta wrote:
> 1. When trying to update two columns
>
> UPDATE myFirstTable SET (fistCol, secCol) = ( SELECT anotherFistCol, anotherSecondCol FROM mySecondTable )

The more common say of expressing this would be something like:

UPDATE myFirstTable a SET
fistCol = b.anotherFistCol,
secCol = b.anotherSecondCol
FROM mySecondTable b
WHERE a.expr = b.expr;

> 2. When changed to (only the parentheses are changed):
>
> UPDATE myFirstTable SET (fistCol, secCol) = ((SELECT anotherFistCol, anotherSecondCol FROM mySecondTable ))
>
> I am getting: ERROR: number of columns does not match number of values

PG is somewhat ad-hoc with its support of its record syntax and
unfortunately doesn't support the above at the moment. The error is
coming from very early on (I think maybe when parsing) and I don't think
it ever gets as far as checking that the value coming back from the
select is a record of the same structure as is on the left hand side.

--
Sam http://samason.me.uk/

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Ivano Luberti 2009-03-17 15:28:14 Re: COPY command question
Previous Message Neanderthelle Jones 2009-03-17 15:17:50 Re: Escaping special characters