pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: John DeSoi <desoi@pgedit.com>
To: Bryce Nesbitt <bryce1@obviously.com>
Cc: pgsql-sql@postgresql.org
Subject: Re: Seeking quick way to clone a row, but give it a new pk.
Date: Thu, 8 Feb 2007 18:09:32 -0600
Message-ID: <5F25919B-421C-4C6B-A749-E4366A506D14@pgedit.com> (raw)
In-Reply-To: <45CA50E6.6090006@obviously.com>
References: <45CA50E6.6090006@obviously.com>

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




view thread (8+ messages)

Message-ID: <5F25919B-421C-4C6B-A749-E4366A506D14@pgedit.com>
Permalink:  ../5F25919B-421C-4C6B-A749-E4366A506D14@pgedit.com/
Also on:    postgresql.org/message-id/5F25919B-421C-4C6B-A749-E4366A506D14@pgedit.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: desoi@pgedit.com, bryce1@obviously.com
  Subject: Re: Seeking quick way to clone a row, but give it a new pk.
  In-Reply-To: <5F25919B-421C-4C6B-A749-E4366A506D14@pgedit.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox