Resp.: How to catch the id in a INSERT INTO ... RETURNING function?

From: Osvaldo Kussama <osvaldo(dot)kussama(at)gmail(dot)com>
To: A B <gentosaker(at)gmail(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Resp.: How to catch the id in a INSERT INTO ... RETURNING function?
Date: 2009-02-01 13:04:18
Message-ID: 690707f60902010504u13c0cd20m2f85e8b42e7ad400@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

2009/2/1, A B <gentosaker(at)gmail(dot)com>:
> Hi.
> I have a table foo(id serial primary key, b int); and I want an insert
> function
>
> create or replace function insert_to_foo(bvalue integer) returns integer as
> declare
> newindex integer;
> begin
> ... insert into foo (a,b) values (default,bvalue) returning id
> .... <---- THIS LINE
> -- do more with newindex here
> return newindex;
> end;
>
>
> Well, the problem is that I want the id of the new post to be saved
> into the newindex variable for further actions. But how do I catch the
> value into the variable?
> Should I do:
>
> select id from insert into foo (a,b) values (default,bvalue) returning id;
> ?
>

Try:

INSERT ... RETURNING expressions INTO [STRICT] target;

38.5.3. Executing a Query with a Single-Row Result
http://www.postgresql.org/docs/current/interactive/plpgsql-statements.html

Osvaldo

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Gerhard Heift 2009-02-01 13:05:31 Re: How to catch the id in a INSERT INTO ... RETURNING function?
Previous Message Thomas Kellerer 2009-02-01 12:47:01 Warm Standby question