X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 65D755E472F for ; Thu, 2 Sep 2004 10:42:13 -0300 (ADT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 62726-02 for ; Thu, 2 Sep 2004 13:42:11 +0000 (GMT) Received: from wolff.to (wolff.to [66.93.249.74]) by svr1.postgresql.org (Postfix) with SMTP id 27D355E46E9 for ; Thu, 2 Sep 2004 10:42:08 -0300 (ADT) Received: (qmail 12952 invoked by uid 500); 2 Sep 2004 13:51:08 -0000 Date: Thu, 2 Sep 2004 08:51:08 -0500 From: Bruno Wolff III To: ogjunk-pgjedan@yahoo.com Cc: pgsql-sql@postgresql.org Subject: Re: copy old column's values to new column Message-ID: <20040902135108.GA12899@wolff.to> Mail-Followup-To: ogjunk-pgjedan@yahoo.com, pgsql-sql@postgresql.org References: <20040902131647.26244.qmail@web12707.mail.yahoo.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20040902131647.26244.qmail@web12707.mail.yahoo.com> User-Agent: Mutt/1.5.6i X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.0 tagged_above=0.0 required=5.0 tests= X-Spam-Level: X-Archive-Number: 200409/25 X-Sequence-Number: 18871 On Thu, Sep 02, 2004 at 06:16:47 -0700, ogjunk-pgjedan@yahoo.com wrote: > Hello, > > I need some basic SQL help. I added a new column to an existing table, > and now I need to copy values from one of the old columns to this new > columns. > > I need something like this: > > FOR pvId IN SELECT id FROM preference_value LOOP > update preference_value SET display_value = (select value from > preference_value where id=pvId) where id=pvId; > END LOOP; > > I tried running this from psql, but it didn't work (I suspect FOR can > be used in functions, which I don't know how to write in PG, yet). > > Is there a simple way to do this? Assuming you are just copying "value" to "display_value", can't you just do: UPDATE preference_value SET display_value = value;