pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Get the last inserted id
5+ messages / 4 participants
[nested] [flat]

* Get the last inserted id
@ 2008-04-11 17:23  Nacef LABIDI <nacef.l@gmail.com>
  0 siblings, 3 replies; 5+ messages in thread

From: Nacef LABIDI @ 2008-04-11 17:23 UTC (permalink / raw)
  To: pgsql-sql

Hi all,

I was wondering if postgres can return the last ID inserted for a table
which the primary key is autoincremented. I need to retrieve this ID in my
code to continue processing on that inserted row.

Thanks to all

^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: Get the last inserted id
@ 2008-04-11 17:41  Scott Marlowe <scott.marlowe@gmail.com>
  parent: Nacef LABIDI <nacef.l@gmail.com>
  2 siblings, 1 reply; 5+ messages in thread

From: Scott Marlowe @ 2008-04-11 17:41 UTC (permalink / raw)
  To: Nacef LABIDI <nacef.l@gmail.com>; +Cc: pgsql-sql

On Fri, Apr 11, 2008 at 11:23 AM, Nacef LABIDI <nacef.l@gmail.com> wrote:
> Hi all,
>
> I was wondering if postgres can return the last ID inserted for a table
> which the primary key is autoincremented. I need to retrieve this ID in my
> code to continue processing on that inserted row.

smarlowe=# create table test (a serial primary key, b text);
NOTICE:  CREATE TABLE will create implicit sequence "test_a_seq" for
serial column "test.a"
NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index
"test_pkey" for table "test"
CREATE TABLE
smarlowe=# insert into test (a,b) values (DEFAULT,'test') returning a;
 a
---
 1
(1 row)

INSERT 0 1

Note that there are other ways of doing it, but I really like the
returning clause.



^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: Get the last inserted id
@ 2008-04-11 17:43  Nacef LABIDI <nacef.l@gmail.com>
  parent: Scott Marlowe <scott.marlowe@gmail.com>
  0 siblings, 0 replies; 5+ messages in thread

From: Nacef LABIDI @ 2008-04-11 17:43 UTC (permalink / raw)
  To: Scott Marlowe <scott.marlowe@gmail.com>; +Cc: pgsql-sql

Nice idea but how can I use this in a frontend that I am developing using
Delphi and ADO objects ?

On Fri, Apr 11, 2008 at 7:41 PM, Scott Marlowe <scott.marlowe@gmail.com>
wrote:

>  On Fri, Apr 11, 2008 at 11:23 AM, Nacef LABIDI <nacef.l@gmail.com> wrote:
> > Hi all,
> >
> > I was wondering if postgres can return the last ID inserted for a table
> > which the primary key is autoincremented. I need to retrieve this ID in
> my
> > code to continue processing on that inserted row.
>
> smarlowe=# create table test (a serial primary key, b text);
> NOTICE:  CREATE TABLE will create implicit sequence "test_a_seq" for
> serial column "test.a"
> NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index
> "test_pkey" for table "test"
> CREATE TABLE
> smarlowe=# insert into test (a,b) values (DEFAULT,'test') returning a;
>  a
> ---
>  1
> (1 row)
>
> INSERT 0 1
>
> Note that there are other ways of doing it, but I really like the
> returning clause.
>

^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: Get the last inserted id
@ 2008-04-11 17:48  A. Kretschmer <andreas.kretschmer@schollglas.com>
  parent: Nacef LABIDI <nacef.l@gmail.com>
  2 siblings, 0 replies; 5+ messages in thread

From: A. Kretschmer @ 2008-04-11 17:48 UTC (permalink / raw)
  To: pgsql-sql

am  Fri, dem 11.04.2008, um 19:23:04 +0200 mailte Nacef LABIDI folgendes:
> Hi all,
>  
> I was wondering if postgres can return the last ID inserted for a table which
> the primary key is autoincremented. I need to retrieve this ID in my code to
> continue processing on that inserted row.

Either with RETURNING:
http://www.postgresql.org/docs/current/static/sql-insert.html

or with currval():
http://www.postgresql.org/docs/current/static/functions-sequence.html


HTH, Andreas
-- 
Andreas Kretschmer
Kontakt:  Heynitz: 035242/47150,   D1: 0160/7141639 (mehr: -> Header)
GnuPG-ID:   0x3FFF606C, privat 0x7F4584DA   http://wwwkeys.de.pgp.net



^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: Get the last inserted id
@ 2008-04-11 17:57  Andrew Sullivan <ajs@crankycanuck.ca>
  parent: Nacef LABIDI <nacef.l@gmail.com>
  2 siblings, 0 replies; 5+ messages in thread

From: Andrew Sullivan @ 2008-04-11 17:57 UTC (permalink / raw)
  To: pgsql-sql

On Fri, Apr 11, 2008 at 07:23:04PM +0200, Nacef LABIDI wrote:
> Hi all,
> 
> I was wondering if postgres can return the last ID inserted for a table
> which the primary key is autoincremented. I need to retrieve this ID in my
> code to continue processing on that inserted row.

Use select currval() on the sequence name.

A



^ permalink  raw  reply  [nested|flat] 5+ messages in thread


end of thread, other threads:[~2008-04-11 17:57 UTC | newest]

Thread overview: 5+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2008-04-11 17:23 Get the last inserted id Nacef LABIDI <nacef.l@gmail.com>
2008-04-11 17:41 ` Scott Marlowe <scott.marlowe@gmail.com>
2008-04-11 17:43   ` Nacef LABIDI <nacef.l@gmail.com>
2008-04-11 17:48 ` A. Kretschmer <andreas.kretschmer@schollglas.com>
2008-04-11 17:57 ` Andrew Sullivan <ajs@crankycanuck.ca>

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