pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: "D'Arcy" "J.M." Cain <darcy@druid.net>
To: tpham@mail.priority.net
Cc: pgsql-sql@postgreSQL.org
Subject: Re: [SQL] Getting primary key from insert statement
Date: Wed, 2 Jun 1999 18:54:33 -0400 (EDT)
Message-ID: <m10pJu5-0000bIC@druid.net> (raw)
In-Reply-To: <551413BBBB03D111BF880060B01AACCE0B38FA@home.priority.net>

Thus spake Pham, Thinh
> Hi, is there a way you can get back the primary key (serialized) from an
> insert you just do on a table. For example i have a table called "order" and
> i want to get the pid of that record so i can use it in the "orderdetail"
> table. It would be nice if that same insert statement could also return the
> pid.
> 
> Table structure:
> create table order (pid serial, orddate date);

You realize that order is a keyword, right?

> Insert statement:
> insert into order (date) values ('6/2/1999')

Assuming you change order to something else, the insert returns the OID
of the record just added.  In psql this appears as something like this.

INSERT 36448 1

So, just read back the record.

SELECT pid FROM table WHERE OID = 36448;

The various interfaces give you methods to get this value back.  Read the
docs for whichever interface you are using.

-- 
D'Arcy J.M. Cain <darcy@{druid|vex}.net>   |  Democracy is three wolves
http://www.druid.net/darcy/                |  and a sheep voting on
+1 416 424 2871     (DoD#0082)    (eNTP)   |  what's for dinner.



view thread (14+ messages)  latest in thread

Message-ID: <m10pJu5-0000bIC@druid.net>
Permalink:  ../m10pJu5-0000bIC@druid.net/
Also on:    postgresql.org/message-id/m10pJu5-0000bIC@druid.net

 · 

reply

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 from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-sql@postgresql.org
  Cc: darcy@druid.net, tpham@mail.priority.net
  Subject: Re: [SQL] Getting primary key from insert statement
  In-Reply-To: <m10pJu5-0000bIC@druid.net>
  References: <551413BBBB03D111BF880060B01AACCE0B38FA@home.priority.net> <m10pJu5-0000bIC@druid.net>

* 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