Re: plpgsql - Insert from a record variable? - Mailing list pgsql-sql

From Tom Lane
Subject Re: plpgsql - Insert from a record variable?
Date
Msg-id 17840.1087670348@sss.pgh.pa.us
Whole thread Raw
In response to Re: plpgsql - Insert from a record variable?  ("Riccardo G. Facchini" <abief_ag_-postgresql@yahoo.com>)
Responses Re: plpgsql - Insert from a record variable?  ("Phil Endecott" <spam_from_postgresql_sql@chezphil.org>)
List pgsql-sql
"Riccardo G. Facchini" <abief_ag_-postgresql@yahoo.com> writes:
> --- Phil Endecott <__> wrote:
>> I'm writing a simple plpgsql function that selects a row from a
>> table, modifies it slightly, and inserts the modified version.  This
>> sounds simple, but I can't find how to do the insert in a simple
>> generic fashion: although SELECT has a form that puts the result in a
>> record variable, INSERT doesn't seem to have anything similar.

> I'm not sure if it will work, but have you tried [snip]

> declare r record;
>   insert into table select r.*;

I don't know of any way to handle this in existing releases ---
plpgsql's support for rowtype variables is just too limited.
However, in 7.5 you will be able to do either of
insert into table select r.*;insert into table values(r.*);

so long as r is declared to be of a named rowtype (not just RECORD;
that doesn't have a known field list to plan the query with).  For
instance I was just experimenting with this test case:

create or replace function fooey(int) returns void as '
declare r foo;
begin select * into r from foo where id = $1; r.f1 = ''baz''; insert into foo select r.*; return;
end' language plpgsql;
        regards, tom lane


pgsql-sql by date:

Previous
From: Peter Eisentraut
Date:
Subject: Re: In 7.4 ensure you have DEFAULT now () with no spaces
Next
From: Tom Lane
Date:
Subject: Re: In 7.4 ensure you have DEFAULT now () with no spaces