From: Richard Huxton <dev@archonet.com>
To: Graf László <graf.laszlo@axis.hu>
Cc: pgsql-sql@postgresql.org
Subject: Re: Returning with the inserted id
Date: Fri, 02 Sep 2005 10:33:36 +0100
Message-ID: <43181C70.4040600@archonet.com> (raw)
In-Reply-To: <431816D1.4060905@axis.hu>
References: <431816D1.4060905@axis.hu>
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
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients using the --to and --cc options:
reply via email
To: pgsql-sql@postgresql.org
Cc: dev@archonet.com, graf.laszlo@axis.hu
Subject: Re: Returning with the inserted id
In-Reply-To: <43181C70.4040600@archonet.com>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox