Re: Copy rows, remember old and new pkey

Поиск
Список
Период
Сортировка
От Merlin Moncure
Тема Re: Copy rows, remember old and new pkey
Дата
Msg-id CAHyXU0wjCQuFQDFr9rwMgSJPtshDXogq+6QAYQFmZa9gL=-q0A@mail.gmail.com
обсуждение исходный текст
Ответ на Copy rows, remember old and new pkey  ("Vlad K." <vlad@haronmedia.com>)
Ответы Re: Copy rows, remember old and new pkey  ("Vlad K." <vlad@haronmedia.com>)
Список pgsql-general
On Mon, Nov 26, 2012 at 3:35 AM, Vlad K. <vlad@haronmedia.com> wrote:
>
> 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;

I don't think you need a CTE or a loop unless you want to delete the
old rows.  Why not (hard to say exactly how it will work):
INSERT INTO a(old_id, new_id, ...)
SELECT id, new_id(), ...
from a;
?

merlin


В списке pgsql-general по дате отправления:

Предыдущее
От: Doug Hunley
Дата:
Сообщение: Re: backslash encoded data changed during upgrade?
Следующее
От: Mark Volz
Дата:
Сообщение: Adding Default Privileges to a schema for a role