pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Jeff Eckermann <jeff_eckermann@yahoo.com>
To: friedrich nietzsche <nietzsche_psql@yahoo.it>
To: pgsql-sql <pgsql-sql@postgresql.org>
Subject: Re: Retrieving the new nextval...
Date: Wed, 28 Aug 2002 11:33:43 -0700 (PDT)
Message-ID: <20020828183343.98562.qmail@web20809.mail.yahoo.com> (raw)
In-Reply-To: <20020828163245.98872.qmail@web12901.mail.yahoo.com>


--- friedrich nietzsche <nietzsche_psql@yahoo.it>
wrote:
> Hi all,
> I'm in trouble with the same problem, but in PHP..
> With your solution, I cannot be totally sure that
> last
> inserted raw was mine...
> Because I'm on a web page, it could be that, as soon
> as I've inserted my record, another one do an
> insertion, so I would get the wrong ID...

"currval" will return the last value used _for the
current connection_.  But if you want to be absolutely
sure, instead call "nextval" before doing your insert,
and use the returned value explicitly.


> does transactions resolve this, in Psql???
> I thought to solve it with a similiar solution,
> working in transactions inserting a raw and
> immedialtly after read from DB last raw, but who
> assure me that all will go right??
> If I was on a server app., I (and you, if it is your
> case) would insert a timestamp, and then I'd select
> from table where timestamp = mysavedtime;
> But in my case there could be two or more equals
> timestamp, cause there's not only one application
> working with DB...
> I'm still reading, searching, trying...
> ciao
> danilo
> 
> 
> 
>  --- Kevin Brannen <kevinb@nurseamerica.net> ha
> scritto: > 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
> R
> 
>
______________________________________________________________________
> Yahoo! Musica: notizie, recensioni, classifiche,
> speciali multimediali
>
http://it.yahoo.com/mail_it/foot/?http://it.music.yahoo.com/
> 
> ---------------------------(end of
> broadcast)---------------------------
> TIP 1: subscribe and unsubscribe commands go to
majordomo@postgresql.org


__________________________________________________
Do You Yahoo!?
Yahoo! Finance - Get real-time stock quotes
http://finance.yahoo.com



view thread (3+ messages)  latest in thread

Message-ID: <20020828183343.98562.qmail@web20809.mail.yahoo.com>
Permalink:  ../20020828183343.98562.qmail@web20809.mail.yahoo.com/
Also on:    postgresql.org/message-id/20020828183343.98562.qmail@web20809.mail.yahoo.com

 · 

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: jeff_eckermann@yahoo.com, nietzsche_psql@yahoo.it
  Subject: Re: Retrieving the new nextval...
  In-Reply-To: <20020828183343.98562.qmail@web20809.mail.yahoo.com>

* 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