Received: from localhost (unknown [200.46.204.183]) by developer.postgresql.org (Postfix) with ESMTP id 833DF2E002F for ; Fri, 11 Apr 2008 14:44:01 -0300 (ADT) Received: from developer.postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 25729-10 for ; Fri, 11 Apr 2008 14:43:58 -0300 (ADT) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.5 Received: from wa-out-1112.google.com (wa-out-1112.google.com [209.85.146.178]) by developer.postgresql.org (Postfix) with ESMTP id F423E2E0032 for ; Fri, 11 Apr 2008 14:43:57 -0300 (ADT) Received: by wa-out-1112.google.com with SMTP id n7so485276wag.5 for ; Fri, 11 Apr 2008 10:43:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:received:received:message-id:date:from:to:subject:cc:in-reply-to:mime-version:content-type:references; bh=49tB7AIAhYRdYAhLgcdRhfXAbN2l4w0MFa8TcMa0hWI=; b=nPCzKUYk0jQr+nxmjXOpYGBumf8UN6TetplBEiIDtWwTIVulEGoOp6wA/owum2u3EVpWzfq7787rTKsswS50RKZMvaH6uddp/w6PG4sTqJP4kEYC0+fP1ygtFsAVCpIYQ66ekHcfoNvPmS1oBhn37eEIwFMVgazY8P4oUKm9uO8= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=message-id:date:from:to:subject:cc:in-reply-to:mime-version:content-type:references; b=HZL75PHd5Bx7zU56o0MzeLyg7/7vqJpriB5voKjIS0OWsHSMGLPvH1IOQXm/GZ27LM5azp+SNRBzTClgm/qb0ZlgyQjgjKU8aBzVMTssiCvTIlwxIKHMtzkStmx9dRkNw76ZMhHNkiac7JSIYqZBoTRTP473p09uuI33rHCM/D0= Received: by 10.114.110.12 with SMTP id i12mr3862308wac.73.1207935836868; Fri, 11 Apr 2008 10:43:56 -0700 (PDT) Received: by 10.70.25.5 with HTTP; Fri, 11 Apr 2008 10:43:56 -0700 (PDT) Message-ID: Date: Fri, 11 Apr 2008 19:43:56 +0200 From: "Nacef LABIDI" To: "Scott Marlowe" Subject: Re: Get the last inserted id Cc: pgsql-sql@postgresql.org In-Reply-To: MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="----=_Part_11003_13294173.1207935836808" References: X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0.001 tagged_above=0 required=5 tests=HTML_MESSAGE=0.001 X-Spam-Level: X-Archive-Number: 200804/78 X-Sequence-Number: 30617 ------=_Part_11003_13294173.1207935836808 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline 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 wrote: > On Fri, Apr 11, 2008 at 11:23 AM, 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. > > 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. > ------=_Part_11003_13294173.1207935836808 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline 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.

------=_Part_11003_13294173.1207935836808--