Re: How to update record in a specified order

From: Josh Berkus <josh(at)agliodbs(dot)com>
To: Jean-Luc Lachance <jllachan(at)nsd(dot)ca>
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: How to update record in a specified order
Date: 2002-08-09 22:12:59
Message-ID: 200208091512.59239.josh@agliodbs.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

JLL,

So, you want to update a field with a NEXTVAL counter, but with the counter
ordered by another column?

If so, you will have to use a procedure. Ordering your UPDATEs is not part
of SQL -- it requires a procedural element. Here's a simple procedure (you
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 = NEXTVAL(''my_sequence'')
WHERE my_table.id = v_rec.id;
END LOOP;
RETURN ''Done updating.'';
END;'
LANGUAGE 'plpgsql';

--
-Josh Berkus
Aglio Database Solutions
San Francisco

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Josh Berkus 2002-08-09 22:15:28 Re: retrieving all rows from a "tree" in one select - how ?
Previous Message Adam Erickson 2002-08-09 22:04:23 Re: retrieving all rows from a "tree" in one select - how ?