Elegant copy of a row using PL

Started by richard lavoieabout 19 years ago3 messagesgeneral
Jump to latest
#1richard lavoie
richard_lavoie@gmx.de

Hi!

I'm not sure about the English terminology for that so I'm sorry if I made a
mistake on the subject and on this message.

I've a table with 50 colums. I want to copy a certain row using PL and change only 2 values. The way to do it with insert is to long. Is there any other elegant way?

Thank you very much
Richard
--
"Feel free" - 5 GB Mailbox, 50 FreeSMS/Monat ...
Jetzt GMX ProMail testen: http://www.gmx.net/de/go/promail

#2Merlin Moncure
mmoncure@gmail.com
In reply to: richard lavoie (#1)
Re: Elegant copy of a row using PL

On 1/16/07, richard lavoie <richard_lavoie@gmx.de> wrote:

Hi!

I'm not sure about the English terminology for that so I'm sorry if I made a
mistake on the subject and on this message.

I've a table with 50 colums. I want to copy a certain row using PL and change only 2 values. The way to do it with insert is to long. Is there any other elegant way?

the basic methodology is to:

insert select into a scratch table;
update scratch table;
insert select back into real_table;

scratch can be a persistent table (remember to truncate it) or a temp
table. if it is a temp, remember to create it before you call your pl
for the first time in a session.

merlin

#3Tom Lane
tgl@sss.pgh.pa.us
In reply to: Merlin Moncure (#2)
Re: Elegant copy of a row using PL

"Merlin Moncure" <mmoncure@gmail.com> writes:

On 1/16/07, richard lavoie <richard_lavoie@gmx.de> wrote:

I've a table with 50 colums. I want to copy a certain row using PL and change only 2 values. The way to do it with insert is to long. Is there any other elegant way?

the basic methodology is to:

insert select into a scratch table;
update scratch table;
insert select back into real_table;

scratch can be a persistent table (remember to truncate it) or a temp
table. if it is a temp, remember to create it before you call your pl
for the first time in a session.

Also, I think in 8.2 you could use a record variable in plpgsql.

declare r record;

select * into r from src where ...;
r.foo = whatever;
r.bar = whatever;
insert into dest values(r.*);

regards, tom lane