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

Re: how to get last inserted id

From: "Albert REINER" <areiner(at)tph(dot)tuwien(dot)ac(dot)at>
To: pgsql-novice(at)postgresql(dot)org
Subject: Re: how to get last inserted id
Date: 2000-12-29 18:08:50
Message-ID: 20001229190850.B435@frithjof (view raw or flat)
Thread:
Lists: pgsql-novice
On Fri, Dec 29, 2000 at 12:36:46PM -0500, Brett W. McCoy wrote:
> On Fri, 29 Dec 2000, chris G√ľnther wrote:
> 
> > How can I find out the last inserted ID of a field with a serial on it???
> 
> currval("sequencename");

currval() will return the last ID inserted from your connection, or
existing before, but without those inserted from other concurrent
connections. This is what you usually want.

But if, for some reason, you are interested just in "what is the last
ID _any_ backend inserted" (which, I think, is the literal meaning of
the question), you will have to look for the maximum value of the
serial field. Usually, though, you should use currval().

Albert.


-- 

--------------------------------------------------------------------------
Albert Reiner                                   <areiner(at)tph(dot)tuwien(dot)ac(dot)at>
Deutsch       *       English       *       Esperanto       *       Latine
--------------------------------------------------------------------------

In response to

pgsql-novice by date

Next:From: chris GntherDate: 2000-12-29 19:36:40
Subject: how to use data type interval
Previous:From: Brett W. McCoyDate: 2000-12-29 17:36:46
Subject: Re: how to get last inserted id

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