Received: from maia.hub.org (maia-5.hub.org [200.46.204.29]) by mail.postgresql.org (Postfix) with ESMTP id 653511337B50; Tue, 26 Oct 2010 06:27:55 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.29]) (amavisd-maia, port 10024) with ESMTP id 97139-04; Tue, 26 Oct 2010 09:27:44 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from exchange.corporate.connx.com (exchange.corporate.connx.com [208.79.149.203]) by mail.postgresql.org (Postfix) with ESMTP id D847D1337B4E; Tue, 26 Oct 2010 06:27:42 -0300 (ADT) Received: from EXCHANGE.corporate.connx.com ([2002:d04f:95cb::d04f:95cb]) by EXCHANGE.corporate.connx.com ([2002:d04f:95cb::d04f:95cb]) with mapi id 14.01.0218.012; Tue, 26 Oct 2010 02:27:16 -0700 From: Dann Corbit To: 'venkat' , Alban Hertroys CC: "pgsql-general@postgresql.org" , "pgsql-sql@postgresql.org" Subject: Re: How to update multiple rows Thread-Topic: [GENERAL] How to update multiple rows Thread-Index: AQHLdO5jJwNZtknubkuD6W2UmBI1r5NS8+Dw Date: Tue, 26 Oct 2010 09:27:15 +0000 Message-ID: <87F42982BF2B434F831FCEF4C45FC33E42068819@EXCHANGE.corporate.connx.com> References: <7C02F18C-3999-4670-82D8-E5168DECA910@solfertje.student.utwente.nl> In-Reply-To: Accept-Language: en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-originating-ip: [2002:d04f:95b8::d04f:95b8] Content-Type: multipart/alternative; boundary="_000_87F42982BF2B434F831FCEF4C45FC33E42068819EXCHANGEcorpora_" MIME-Version: 1.0 X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.909 tagged_above=-5 required=5 tests=BAYES_00=-1.9, HTML_MESSAGE=0.001, T_RP_MATCHES_RCVD=-0.01 X-Spam-Level: X-Archive-Number: 201010/928 X-Sequence-Number: 168840 --_000_87F42982BF2B434F831FCEF4C45FC33E42068819EXCHANGEcorpora_ Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable If you do not understand what you are doing, it might be a good idea to fin= d someone in your organization who understands SQL. You were given the suggestion UPDATE SET FROM ... which is documented here: http://www.postgresql.org/docs/9.0/interactive/sql-update.html In the end, your query should probably end up as an update with an inner jo= in in the FROM section. Do you know what the unique indexes are on the tab= les in question? If you do not use unique indexes in the join, or some oth= er way to ensure that there is a one to one correspondence, you will be alt= ering great big patches of data. If you are performing an update query, which modifies the data, you should = be very careful to get it right. It is risky to have someone else write yo= ur query for you, because it is your organization that is intimately famili= ar with your data. Do you have any SQL experts in your company? These two kinds of knowledge = are essential: 1) Knowledge of SQL 2) Knowledge of your company's data Without both of those qualifications, the query produced will not create co= rrect results. From: pgsql-general-owner@postgresql.org [mailto:pgsql-general-owner@postgr= esql.org] On Behalf Of venkat Sent: Tuesday, October 26, 2010 2:15 AM To: Alban Hertroys Cc: pgsql-general@postgresql.org; pgsql-sql@postgresql.org Subject: Re: [GENERAL] How to update multiple rows Dear Alban, Thanks for your great response.I am not able to compile the query which yo= u have given..I am not able to understand.Please alter my code. (select kasarano from parcelsdata), murabano=3D(select murabano from parcelsdata), the_geom =3D (select (st_dump(st_polygonize(st_union(s= t_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 =3D 113 GROUP BY gid,kasarano,murabano) if i compile above code , its giving me 2 records.. and when i try to updat= e the table i am getting using below code... update parcelsdata set gid=3D(select random() * 10), kasarano=3D(select kasarano from parcelsdata), murabano=3D(select murabano from parcelsdata), the_geom =3D (select (st_dump(st_polygonize(st_union(s= t_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 =3D 113 GROUP BY gid,kasarano,murabano) I am getting below error.. "ERROR: more than one row returned by a subquery used as an expression SQL state: 21000" Please let me know where I am doing wrong.. guide me how to update those mu= ltiple records..I am waiting for your great response. Thanks and Regards, Venkat On Tue, Oct 26, 2010 at 1:32 PM, Alban Hertroys > wrote: 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 que= ry.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 s= elected 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=3D(select random() * 10), > kasarano=3D(select kasarano from parcelsdata), > murabano=3D(select murabano from parcelsdata), > the_geom =3D (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, th= e_geom from > (select 100 as the_length, * from parcelsdata) a) b > where gid =3D 113 GROUP BY gid,kasarano,murabano) > > where kasarano=3D'1' and murabano=3D'119' You would rewrite that to, for example: update parcelsdata set gid =3D random() * 10, kasarano =3D pd2.kasarano, murabano =3D pd2.murabano from parcelsdata pd2 where id =3D pd2.id -- substitute for whatever your primary = key/condition is and kasarano =3D '1' and murabano =3D '119' Yeah, I left out the geometry thing as I'm too lazy to figure out where you= r brackets start and end ;) Alban Hertroys -- Screwing up is an excellent way to attach something to the ceiling. !DSPAM:1184,4cc68b1610291250718568! --_000_87F42982BF2B434F831FCEF4C45FC33E42068819EXCHANGEcorpora_ Content-Type: text/html; charset="us-ascii" Content-Transfer-Encoding: quoted-printable

If you do not understand what you are doing, it might be a g= ood idea to find someone in your organization who understands SQL.

You were given the suggestion UPDATE <table> SET <c= olumn list > FROM <from list > … which is documented here:

http://www.postgresql.org/docs/9.0/interactive/sql-update.ht= ml

In the end, your query should probably end up as an update w= ith an inner join in the FROM section.  Do you know what the unique in= dexes are on the tables in question?  If you do not use unique indexes in the join, or some o= ther way to ensure that there is a one to one correspondence, you will be a= ltering great big patches of data.

 

If you are performing an update query, which modifies the da= ta, you should be very careful to get it right.  It is risky to have s= omeone else write your query for you, because it is your organization that is intimately familiar with = your data.

 

Do you have any SQL experts in your company?  These two= kinds of knowledge are essential: 1) Knowledge of SQL 2) Knowledge of your= company’s data

Without both of those qualifications, the query produced wil= l not create correct results.

 

 

From: pgsql-ge= neral-owner@postgresql.org [mailto:pgsql-general-owner@postgresql.org] On Behalf Of venkat
Sent: Tuesday, October 26, 2010 2:15 AM
To: Alban Hertroys
Cc: pgsql-general@postgresql.org; pgsql-sql@postgresql.org
Subject: Re: [GENERAL] How to update multiple rows
=

 

Dear Alban,

 

 Thanks for your = great response.I am not able to compile the query which you have given..I a= m not able to understand.Please alter my code.

 

(select= kasarano from parcelsdata),
           &= nbsp;         murabano=3D(select murabano from parcelsd= ata),
           &= nbsp;         the_geom =3D (select (st_dump(st_polygoni= ze(st_union(st_boundary(the_geom), the_line)))).geom as the_geom from
           &= nbsp;   (select gid,kasarano,murabano,st_linefromtext('LINESTRING('
           &= nbsp;   ||(st_xmin(the_geom)-1)::text||'
           &= nbsp;   '||(st_ymax(the_geom)-the_length)||',
           &= nbsp;   '||st_xmax(the_geom)+1||'
           &= nbsp;   '||st_ymax(the_geom)-the_length||')',24047) as the_line, the_g= eom from
           &= nbsp;   (select 100 as the_length, * from parcelsdata) a) b
           &= nbsp;           where gid =3D 113 GROUP BY gid,kas= arano,murabano)

if i co= mpile above code , its giving me 2 records.. and when i try to update the t= able i am getting  using below code...

 

update = parcelsdata set gid=3D(select random() * 10),
           &= nbsp;         kasarano=3D(select kasarano from parcelsd= ata),
           &= nbsp;         murabano=3D(select murabano from parcelsd= ata),
           &= nbsp;         the_geom =3D (select (st_dump(st_polygoni= ze(st_union(st_boundary(the_geom), the_line)))).geom as the_geom from
           &= nbsp;   (select gid,kasarano,murabano,st_linefromtext('LINESTRING('
           &= nbsp;   ||(st_xmin(the_geom)-1)::text||'
           &= nbsp;   '||(st_ymax(the_geom)-the_length)||',
           &= nbsp;   '||st_xmax(the_geom)+1||'
           &= nbsp;   '||st_ymax(the_geom)-the_length||')',24047) as the_line, the_g= eom from
           &= nbsp;   (select 100 as the_length, * from parcelsdata) a) b
           &= nbsp;           where gid =3D 113 GROUP BY gid,kas= arano,murabano)

 

I am getting below error..

"ERROR: more than= one row returned by a subquery used as an expression

SQL state: 21000"=

 

 

Please let me know whe= re I am doing wrong.. guide me how to update those multiple records..I am w= aiting for your great response.

 

Thanks and Regards,

 

Venkat

On Tue, Oct 26, 2010 at 1:32 PM, Alban Hertroys <= dalroi@solfertje.stu= dent.utwente.nl> wrote:

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

> Dear All,
>
>   I want to update multiple row in single query.I am trying for b= elow query.I am getting error  as
>
> "ERROR: more than one row returned by a subquery used as an expre= ssion
> 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 s= elected 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=3D(select random() * 10),
>                     = kasarano=3D(select kasarano from parcelsdata),
>                     = murabano=3D(select murabano from parcelsdata),
>                     = the_geom =3D (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)&= #43;1||'
>               '||st_ymax(the_geom)-= the_length||')',24047) as the_line, the_geom from
>               (select 100 as the_le= ngth, * from parcelsdata) a) b
>                     =   where gid =3D 113 GROUP BY gid,kasarano,murabano)
>
> where kasarano=3D'1' and murabano=3D'119'

You would rewrite tha= t to, for example:

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

from parcelsdata pd2
where id =3D pd2.id -- subs= titute for whatever your primary key/condition is

and kasarano =3D '1'
and murabano =3D '119'

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

Alban Hertroys

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


!DSPAM:1184,4cc68b1610291250718568!

 

--_000_87F42982BF2B434F831FCEF4C45FC33E42068819EXCHANGEcorpora_--