On Tue, Jun 17, 2008 at 3:46 AM, Klint Gore <[EMAIL PROTECTED]> wrote:
> The only way I could find to make this work is to use a rule and wrap the
> inner "insert returning" in a function.
>
> create or replace function newperson (studentinfo) returns setof person as
> $$
> declare
>  arec person%rowtype;
> begin
>  for arec in
>     insert into person (foo,bar) values ($1.foo,$1.bar) returning *
>  loop
>    -- insert into address (...) values (arec.person_id, $1....)
>    -- insert into phone (...) values (arec.person_id, $1....)
>     return next arec;
>  end loop;
>  return;
> end;
> $$
> language plpgsql volatile;
> create rule atest as on insert to studentinfo do instead (
>  insert into student (person_id) select (select person_id from
> newperson(new));
> );

Here is another question: why does "newperson" have to be a table
function (returning SETOF)?  It seems to work fine for me to do

create or replace function newperson (studentinfo) returns integer as $$
declare
  pid integer;
begin
  insert into person (foo,bar) values ($1.foo,$1.bar) returning
person_id into pid;
  return pid;
end; $$ language plpgsql;

create rule atest as on insert to studentinfo do instead
  insert into student (person_id, baz) values (newperson(new), new.baz);

Mike

-- 
Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general

Reply via email to