Re: Seeking quick way to clone a row, but give it a new pk.

From: John DeSoi <desoi(at)pgedit(dot)com>
To: Bryce Nesbitt <bryce1(at)obviously(dot)com>
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: Seeking quick way to clone a row, but give it a new pk.
Date: 2007-02-09 00:09:32
Message-ID: 5F25919B-421C-4C6B-A749-E4366A506D14@pgedit.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

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

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Tom Lane 2007-02-09 01:54:28 Re: huge disparities in =/IN/BETWEEN performance
Previous Message George Pavlov 2007-02-08 23:57:17 huge disparities in =/IN/BETWEEN performance