Re: Returning with the inserted id

From: Richard Huxton <dev(at)archonet(dot)com>
To: Graf László <graf(dot)laszlo(at)axis(dot)hu>
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: Returning with the inserted id
Date: 2005-09-02 09:33:36
Message-ID: 43181C70.4040600@archonet.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Graf László wrote:
>
> A sequence to hold the id was defined with:
> CREATE SEQUENCE "public"."test_azon_seq"
> INCREMENT 1 MINVALUE 101
> MAXVALUE 9223372036854775807 START 101
> CACHE 1;
>
> The function wich allocates the id and defines the datum is:
> CREATE FUNCTION test_verif() RETURNS trigger AS $test_verif$
> BEGIN
> select into NEW.id nextval('test_azon_seq');
> NEW.datum := current_timestamp;
> RETURN NEW;
> END;
> $test_verif$ LANGUAGE plpgsql;

I take it this is just an example, because you could do this with
DEFAULTs on both columns.

> When I issue an insert (see below) how can I retrieve the
> inserted value of id? I need something like Oracle's returns
> for insert.
>
> insert into "public"."test" (nev) values ('text');

SELECT currval('public.test_azon_seq');

And yes, it will cope with multiple concurrent connections inserting.
--
Richard Huxton
Archonet Ltd

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Stathis Stergou 2005-09-02 11:17:52 cursor "<unnamed portal 1>" already in use
Previous Message Graf László 2005-09-02 09:09:37 Returning with the inserted id