Re: How to update multiple rows

From: Alban Hertroys <dalroi(at)solfertje(dot)student(dot)utwente(dot)nl>
To: venkat <ven(dot)tammineni(at)gmail(dot)com>
Cc: pgsql-general(at)postgresql(dot)org, pgsql-sql(at)postgresql(dot)org
Subject: Re: How to update multiple rows
Date: 2010-10-26 08:02:20
Message-ID: 7C02F18C-3999-4670-82D8-E5168DECA910@solfertje.student.utwente.nl
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general pgsql-sql

On 26 Oct 2010, at 9:07, venkat wrote:

> Dear All,
>
> I want to update multiple row in single query.I am trying for below query.I am getting error as
>
> "ERROR: more than one row returned by a subquery used as an expression
> SQL state: 21000"

You're probably looking for UPDATE table FROM other_table.

That said, I think your subqueries are rather under-constrained - you don't correlate the records in your subqueries to the records you're updating at all! The result will be that all your rows will be based on the last row selected by each subquery. I can't imagine why you'd want that, so I assume you don't.

> Here is my Query.
>
> update parcelsdata set gid=(select random() * 10),
> kasarano=(select kasarano from parcelsdata),
> murabano=(select murabano from parcelsdata),
> the_geom = (select (st_dump(st_polygonize(st_union(st_boundary(the_geom), the_line)))).geom as the_geom from
> (select gid,kasarano,murabano,st_linefromtext('LINESTRING('
> ||(st_xmin(the_geom)-1)::text||'
> '||(st_ymax(the_geom)-the_length)||',
> '||st_xmax(the_geom)+1||'
> '||st_ymax(the_geom)-the_length||')',24047) as the_line, the_geom from
> (select 100 as the_length, * from parcelsdata) a) b
> where gid = 113 GROUP BY gid,kasarano,murabano)
>
> where kasarano='1' and murabano='119'

You would rewrite that to, for example:

update parcelsdata
set gid = random() * 10,
kasarano = pd2.kasarano,
murabano = pd2.murabano

from parcelsdata pd2
where id = pd2.id -- substitute for whatever your primary key/condition is

and kasarano = '1'
and murabano = '119'

Yeah, I left out the geometry thing as I'm too lazy to figure out where your brackets start and end ;)

Alban Hertroys

--
Screwing up is an excellent way to attach something to the ceiling.

!DSPAM:737,4cc68b1c10291756917282!

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Szymon Guz 2010-10-26 08:13:04 what can depend on index
Previous Message venkat 2010-10-26 07:07:44 How to update multiple rows

Browse pgsql-sql by date

  From Date Subject
Next Message venkat 2010-10-26 09:14:48 Re: How to update multiple rows
Previous Message venkat 2010-10-26 07:07:44 How to update multiple rows