Skip site navigation (1) Skip section navigation (2)

Re: How to retrieve the serial number of an INSERT?

From: Bruno Wolff III <bruno(at)wolff(dot)to>
To: Daniel Whelan <merlin(at)ophelan(dot)com>
Cc: pgsql-novice(at)postgresql(dot)org
Subject: Re: How to retrieve the serial number of an INSERT?
Date: 2004-04-28 07:09:49
Message-ID: 20040428070949.GD6901@wolff.to (view raw or flat)
Thread:
Lists: pgsql-novice
On Tue, Apr 27, 2004 at 22:09:26 -0400,
  Daniel Whelan <merlin(at)ophelan(dot)com> wrote:
> I have the following scenario, and I'm sure many others have wished to do 
> something similar. I perform an insert into a table where one field is a 
> SERIAL. I wish to perform several other inserts (on other tables) that will 
> reference the first record based on its SERIAL field. In the application, I 
> wish to name a file based on the SERIAL (I don't wish for postgres to name 
> the file for me, I would just like to get at the number).
> 
> The specific table is listed below. To recap, I wish to perform an insert 
> and also receive the 'id' field value back. Is there any way for me to do 
> this, or is there a better technique for achieving what I wish to do? 
> Thanks!

Unless you have done something odd the function currval('photos_id_seq')
will return the id value of the last record added to photos in the
current session. You can use this function directly in sql statements
so that you don't have to pass the value back to the application in many
cases.

> 
> Daniel
> 
> CREATE TABLE photos (
>    id serial NOT NULL,
>    parent_id integer NOT NULL,
>    photographer_id integer NOT NULL,
>    title character varying,
>    description character varying
> );

In response to

pgsql-novice by date

Next:From: Robert MorganDate: 2004-04-28 07:27:00
Subject: error on starting server
Previous:From: Tom LaneDate: 2004-04-28 05:00:07
Subject: Re: Could not create shared memory segment...(in linux)

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group