Received: from localhost (postgresql.org [64.49.215.8]) by postgresql.org (Postfix) with ESMTP id 1EEBE476099; Wed, 28 Aug 2002 12:09:39 -0400 (EDT) Received: from dilbert.nurseamerica.net (unknown [207.53.237.114]) by postgresql.org (Postfix) with ESMTP id 1A290475F09; Wed, 28 Aug 2002 12:09:37 -0400 (EDT) Received: from alice.nurseamerica.local (airband-216-138-83-219.airband.net [216.138.83.219] (may be forged)) by dilbert.nurseamerica.net (8.11.6/8.11.6) with ESMTP id g7SG9gg24094; Wed, 28 Aug 2002 11:09:42 -0500 Received: from nurseamerica.net ([192.168.0.17]) by alice.nurseamerica.local with Microsoft SMTPSVC(5.0.2195.5329); Wed, 28 Aug 2002 11:09:41 -0500 Message-ID: <3D6CF5B7.90206@nurseamerica.net> Date: Wed, 28 Aug 2002 11:09:27 -0500 From: Kevin Brannen User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.1) Gecko/20020826 X-Accept-Language: en-us, en MIME-Version: 1.0 To: Greg Patnude Cc: pgsql-general@postgresql.org, pgsql-sql@postgresql.org Subject: Re: [SQL] Retrieving the new "nextval" for primary keys.... References: Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit X-OriginalArrivalTime: 28 Aug 2002 16:09:41.0614 (UTC) FILETIME=[511080E0:01C24EAD] X-Virus-Scanned: by AMaViS new-20020517 X-Archive-Number: 200208/1125 X-Sequence-Number: 29468 Greg Patnude wrote: > I am using postgreSQL with Perl::CGI and Perl::DBI::Pg... I would like to be > able to insert a row from my Perl script [$SQL->exec();] and have postgreSQL > return the id of the newly inserted record (new.id) directly to the Perl > script for further processing... Anyone with a solution / idea ??? > > Nearly EVERY table I create in postgreSQL (7.2) has the following minimum > structure: > > create table "tblName" ( > > id int4 primary key nextval ("tblName_id_seq"), > > ..field... > ) You can either do it in 2 statements, something like: $dbh->do("insert into tblName ..."); my ($id) = $dbh->selectrow_array("select currval('tblName_id_seq')"); Or you could create a function which takes the insert statement, and ends with doing a select on the currval (as above) and returning that. As I do the 2 statement approach above, I haven't done a function, but it doesn't look like it would be that hard to do. HTH, Kevin