Re: Re: how to insert multiple rows and get the ids back in a temp table (pgplsql)?

From: Merlin Moncure <mmoncure(at)gmail(dot)com>
To: "abhishek(dot)itbhu2004" <abhishek(dot)itbhu2004(at)gmail(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Re: how to insert multiple rows and get the ids back in a temp table (pgplsql)?
Date: 2011-04-07 13:48:01
Message-ID: BANLkTikC_4F+fUVkoE2T4Tep6ho=UswSEg@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Wed, Apr 6, 2011 at 11:12 PM, abhishek.itbhu2004
<abhishek(dot)itbhu2004(at)gmail(dot)com> wrote:
> I am still new to postgres. Can you please tell the exact syntax for this. I
> tried different things but was not able to retun the ids of the newly
> inserted rows.

in 9.1 with wCTE you will have a very direct way to do this. in 9.0
down, there is only one effective way to do this. you have to wrap
your insert statement in a plain (sql, not plpgsql) function and have
it return results:

create function ins_foo() returns setof int as
$$
insert into foo(v) select 1 from generate_series(1,3) v returning id;
$$ language sql;

That function can be called from plpgsql or from another query:
select array(select ins_foo());

merlin

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Merlin Moncure 2011-04-07 13:53:00 Re: Arrays of arrays
Previous Message Andrew Sullivan 2011-04-07 13:43:11 Re: Protecting stored procedures