pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Kevin Brannen <kevinb@nurseamerica.net>
To: Greg Patnude <GPatnude@adelphia.net>
Cc: pgsql-general@postgresql.org
Cc: pgsql-sql@postgresql.org
Subject: Re: [SQL] Retrieving the new "nextval" for primary keys....
Date: Wed, 28 Aug 2002 11:09:27 -0500
Message-ID: <3D6CF5B7.90206@nurseamerica.net> (raw)
References: <ak6lse$a9a$1@news.hub.org>

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




view thread (4+ messages)  latest in thread

Message-ID: <3D6CF5B7.90206@nurseamerica.net>
Permalink:  ../3D6CF5B7.90206@nurseamerica.net/
Also on:    postgresql.org/message-id/3D6CF5B7.90206@nurseamerica.net

 · 

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 using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: kevinb@nurseamerica.net, GPatnude@adelphia.net, pgsql-general@postgresql.org
  Subject: Re: [SQL] Retrieving the new "nextval" for primary keys....
  In-Reply-To: <3D6CF5B7.90206@nurseamerica.net>

* 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