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

How to retrieve the serial number of an INSERT?

From: Daniel Whelan <merlin(at)ophelan(dot)com>
To: pgsql-novice(at)postgresql(dot)org
Subject: How to retrieve the serial number of an INSERT?
Date: 2004-04-28 02:09:26
Message-ID: 216854109.1083103766@[10.0.2.10] (view raw or whole thread)
Thread:
Lists: pgsql-novice
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!

Daniel

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

Responses

pgsql-novice by date

Next:From: Avi SchwartzDate: 2004-04-28 02:24:26
Subject: Re: opening postgresql first time
Previous:From: Robert MorganDate: 2004-04-28 01:56:53
Subject: opening postgresql first time

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