Received: from localhost (maia-1.hub.org [200.46.204.191]) by postgresql.org (Postfix) with ESMTP id 68F009FB290 for ; Thu, 8 Feb 2007 20:09:38 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.191]) (amavisd-new, port 10024) with ESMTP id 92029-09 for ; Thu, 8 Feb 2007 20:09:34 -0400 (AST) X-Greylist: from auto-whitelisted by SQLgrey-1.7.4 Received: from a2s5.a2hosting.com (a2s5.a2hosting.com [209.124.50.10]) by postgresql.org (Postfix) with ESMTP id 90B3A9F9FEF for ; Thu, 8 Feb 2007 20:09:34 -0400 (AST) Received: from ppp-70-254-34-120.dsl.lgvwtx.swbell.net ([70.254.34.120] helo=[192.168.1.3]) by a2s5.a2hosting.com with esmtpsa (TLSv1:AES128-SHA:128) (Exim 4.63) (envelope-from ) id 1HFJKW-0000uw-F4; Thu, 08 Feb 2007 19:09:32 -0500 In-Reply-To: <45CA50E6.6090006@obviously.com> References: <45CA50E6.6090006@obviously.com> Mime-Version: 1.0 (Apple Message framework v752.3) Content-Type: text/plain; charset=US-ASCII; delsp=yes; format=flowed Message-Id: <5F25919B-421C-4C6B-A749-E4366A506D14@pgedit.com> Cc: pgsql-sql@postgresql.org Content-Transfer-Encoding: 7bit From: John DeSoi Subject: Re: Seeking quick way to clone a row, but give it a new pk. Date: Thu, 8 Feb 2007 18:09:32 -0600 To: Bryce Nesbitt X-Mailer: Apple Mail (2.752.3) X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - a2s5.a2hosting.com X-AntiAbuse: Original Domain - postgresql.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - pgedit.com X-Source: X-Source-Args: X-Source-Dir: X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200702/108 X-Sequence-Number: 27442 A pl/pgsql function can do this easily. Something like this (not tested): create or replace function dup_my_table(old_key text, new_key text) returns text as $$ declare rec my_table; begin; select into rec * from my_table where key_field = old_key; rec.key_field = new_key; insert into my_table values (rec.*); return new_key; end; $$ language plpgsql; On Feb 7, 2007, at 4:21 PM, Bryce Nesbitt wrote: > I need to create some nearly identical copies of rows in a complicated > table. > > Is there a handy syntax that would let me copy a existing row, but > get a > new primary key for the copy? I'd then go in an edit the 1 or 2 > additional columns that differ. The duplicate would be in the same > table as the original. John DeSoi, Ph.D. http://pgedit.com/ Power Tools for PostgreSQL