Received: from localhost (postgresql.org [64.49.215.8]) by postgresql.org (Postfix) with ESMTP id 78791475939 for ; Fri, 9 Aug 2002 18:13:46 -0400 (EDT) Received: from davinci.ethosmedia.com (davinci.ethosmedia.com [209.10.40.250]) by postgresql.org (Postfix) with ESMTP id E1D694754A7 for ; Fri, 9 Aug 2002 18:13:45 -0400 (EDT) Received: from [66.219.92.2] (HELO chocolate-mousse) by davinci.ethosmedia.com (CommuniGate Pro SMTP 3.5.9) with ESMTP id 1606700; Fri, 09 Aug 2002 15:13:47 -0700 Content-Type: text/plain; charset="iso-8859-1" From: Josh Berkus Reply-To: josh@agliodbs.com Organization: Aglio Database Solutions To: Jean-Luc Lachance Subject: Re: How to update record in a specified order Date: Fri, 9 Aug 2002 15:12:59 -0700 X-Mailer: KMail [version 1.4] Cc: pgsql-sql@postgresql.org References: <200208091437.52702.josh@agliodbs.com> <3D543B55.CE5F5DEF@nsd.ca> In-Reply-To: <3D543B55.CE5F5DEF@nsd.ca> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Message-Id: <200208091512.59239.josh@agliodbs.com> X-Virus-Scanned: by AMaViS new-20020517 X-Archive-Number: 200208/173 X-Sequence-Number: 9019 JLL, So, you want to update a field with a NEXTVAL counter, but with the counter= =20 ordered by another column? If so, you will have to use a procedure. Ordering your UPDATEs is not par= t=20 of SQL -- it requires a procedural element. Here's a simple procedure (yo= u=20 debug it): CREATE PROCEDURE add_my_table_counter () RETURNS TEXT AS ' DECLARE v_rec RECORD; BEGIN WHILE v_rec IN SELECT * FROM my_table ORDER BY last_name LOOP UPDATE my_table SET counter_field =3D NEXTVAL(''my_sequence'') WHERE my_table.id =3D v_rec.id; END LOOP; RETURN ''Done updating.''; END;' LANGUAGE 'plpgsql'; --=20 -Josh Berkus Aglio Database Solutions San Francisco