pg.ddx.io pgsql-sql@postgresql.org mailing list archive
help / color / mirror / Atom feedRetrieving the new "nextval" for primary keys....
4+ messages / 4 participants
[nested] [flat]
* Retrieving the new "nextval" for primary keys....
@ 2002-08-24 01:15 Greg Patnude <GPatnude@adelphia.net>
0 siblings, 1 reply; 4+ messages in thread
From: Greg Patnude @ 2002-08-24 01:15 UTC (permalink / raw)
To: pgsql-hackers@postgresql.org; pgsql-general@postgresql.org; pgsql-sql
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...
..field...
..field...
create_dt date default 'CURRENT_DATE',
change_dt timestamptz default 'now()',
active_flag bool default 'TRUE'
)
^ permalink raw reply [nested|flat] 4+ messages in thread
* Re: [SQL] Retrieving the new "nextval" for primary keys....
@ 2002-08-28 16:09 Kevin Brannen <kevinb@nurseamerica.net>
parent: Greg Patnude <GPatnude@adelphia.net>
0 siblings, 1 reply; 4+ messages in thread
From: Kevin Brannen @ 2002-08-28 16:09 UTC (permalink / raw)
To: Greg Patnude <GPatnude@adelphia.net>; +Cc: pgsql-general@postgresql.org; pgsql-sql
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
^ permalink raw reply [nested|flat] 4+ messages in thread
* Re: [SQL] Retrieving the new "nextval" for primary keys....
@ 2002-08-28 16:36 friedrich nietzsche <nietzsche_psql@yahoo.it>
parent: Kevin Brannen <kevinb@nurseamerica.net>
0 siblings, 1 reply; 4+ messages in thread
From: friedrich nietzsche @ 2002-08-28 16:36 UTC (permalink / raw)
To: pgsql-sql; +Cc: pgsql-general@postgresql.org; pgsql-sql
One solution seems to locking table(s),
but I prefer to leave it as last chance...
using table locks, and the trick of writing and
suddenly reading back from DB it probably works,
but it doesn't seems so sexy... :)
ciao
danilo
______________________________________________________________________
Yahoo! Musica: notizie, recensioni, classifiche, speciali multimediali
http://it.yahoo.com/mail_it/foot/?http://it.music.yahoo.com/
^ permalink raw reply [nested|flat] 4+ messages in thread
* Re: [SQL] Retrieving the new "nextval" for primary keys....
@ 2002-09-02 19:50 GB Clark <postgres@vsservices.com>
parent: friedrich nietzsche <nietzsche_psql@yahoo.it>
0 siblings, 0 replies; 4+ messages in thread
From: GB Clark @ 2002-09-02 19:50 UTC (permalink / raw)
To: friedrich nietzsche <nietzsche_psql@yahoo.it>; +Cc: pgsql-sql; pgsql-general@postgresql.org
On Wed, 28 Aug 2002 18:36:10 +0200 (CEST)
friedrich nietzsche <nietzsche_psql@yahoo.it> wrote:
> One solution seems to locking table(s),
> but I prefer to leave it as last chance...
> using table locks, and the trick of writing and
> suddenly reading back from DB it probably works,
> but it doesn't seems so sexy... :)
> ciao
> danilo
>
Why would you have to lock the table? currval() is connection safe.
I would either do the insert and then do a currval() OR do a nextval()
and do the insert. Either one would work. I always just do the insert
and then call currval() to get the current serial number for the connection.
GB
--
GB Clark II | Roaming FreeBSD Admin
gclarkii@VSServices.COM | General Geek
CTHULU for President - Why choose the lesser of two evils?
^ permalink raw reply [nested|flat] 4+ messages in thread
end of thread, other threads:[~2002-09-02 19:50 UTC | newest]
Thread overview: 4+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2002-08-24 01:15 Retrieving the new "nextval" for primary keys.... Greg Patnude <GPatnude@adelphia.net>
2002-08-28 16:09 ` Kevin Brannen <kevinb@nurseamerica.net>
2002-08-28 16:36 ` friedrich nietzsche <nietzsche_psql@yahoo.it>
2002-09-02 19:50 ` GB Clark <postgres@vsservices.com>
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