Copy rows, remember old and new pkey - Mailing list pgsql-general

From Vlad K.
Subject Copy rows, remember old and new pkey
Date
Msg-id 50B337EB.2040909@haronmedia.com
Whole thread Raw
Responses Re: Copy rows, remember old and new pkey
List pgsql-general
Hello list,

I need to make a copy of certain rows in table A and remember the
original pkey and new pkey in some temporary table B. Basically the copy
statement is    INSERT INTO a SELECT * FROM a where a.x=y;

I guess I can do it with a plpgsql function and a FOR loop statement,
because I need two insert statements, the second using returned pkey
from first, but I was wondering if there's a simpler way, perhaps using
WITH?

FOR row IN SELECT * FROM a WHERE a.x=123 LOOP
     INSERT INTO a (x, y, z) VALUES (row.x, row.y, row.z) RETURNING pkey
INTO new_pkey;
     INSERT INTO b (old_id, new_id) VALUES (row.pkey, new_pkey);
END LOOP;


Thanks,

--


.oO V Oo.


Work Hard,
Increase Production,
Prevent Accidents,
and
Be Happy!  ;)



pgsql-general by date:

Previous
From: "Albe Laurenz"
Date:
Subject: Re: Restore postgres to specific time
Next
From: Glyn Astill
Date:
Subject: Re: Npgsql