pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Herouth Maoz <herouth@oumail.openu.ac.il>
To: Pham, Thinh <tpham@mail.priority.net>
To: 'pgsql-sql@postgresql.org' <pgsql-sql@postgreSQL.org>
Subject: Re: [SQL] Getting primary key from insert statement
Date: Sun, 6 Jun 1999 15:31:45 +0300
Message-ID: <l03130304b38014d3b9f7@[147.233.159.109]> (raw)
In-Reply-To: <551413BBBB03D111BF880060B01AACCE0B38FA@home.priority.net>

At 00:46 +0300 on 03/06/1999, Pham, Thinh wrote:


> 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.

Yes. You have to know the name of the sequence which the serial type caused
to be created. You are notified of the name when you create the table. It
would be 'order_pid_seq' in the case you described (remember that ORDER is
a reserved word, though).

Anyway, after you make the insert, you can retrieve the recently-created
value of the sequence, using currval( 'order_pid_seq' ).

This returns the value of the last invocation of nextval( 'order_pid_seq' )
in the current session. When you do an insert, it calls nextval internally.
And thus you will get a proper currval.

The question everybody asks is "but what if somebody else manages to insert
another row before I use currval?". This is not a concern, because currval
always returns the number your own session generated, and not anybody else.

Note that currval will not work until somebody does a nextval on the same
sequence.

Herouth

--
Herouth Maoz, Internet developer.
Open University of Israel - Telem project
http://telem.openu.ac.il/~herutma





view thread (14+ messages)  latest in thread

Message-ID: <l03130304b38014d3b9f7@[147.233.159.109]>
Permalink:  ../l03130304b38014d3b9f7@%5B147.233.159.109%5D/
Also on:    postgresql.org/message-id/l03130304b38014d3b9f7@[147.233.159.109]

 · 

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: herouth@oumail.openu.ac.il, tpham@mail.priority.net
  Subject: Re: [SQL] Getting primary key from insert statement
  In-Reply-To: <l03130304b38014d3b9f7@[147.233.159.109]>
  References: <551413BBBB03D111BF880060B01AACCE0B38FA@home.priority.net> <l03130304b38014d3b9f7@[147.233.159.109]>

* 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