pg.ddx.io  pgsql-interfaces@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Bryan White <bryan@arcamax.com>
To: pgsql-interfaces@postgreSQL.org
Subject: Re: [INTERFACES] ecpg and getting just assigned serial number
Date: Mon, 13 Sep 1999 09:03:54 -0400
Message-ID: <000f01befde8$6ec81940$22d260d1@arcamax.com> (raw)
References: <2567.937152101@sss.pgh.pa.us>

> theofilu@eunet.at (Theofilu Andreas) writes:
> > to Informix's 'esqlc'. However. I have a table with a field of type
> > 'serial'. Now I've no problem to insert any sentences into this table,
but
> > immediately after inserting a new sentence I need the newly assigned
number
> > to the serial field in the table. How can I access this number?
>
> Postgres doesn't return that number automatically; you'll have to run a
> separate query to find out what was inserted.  You could do something
> like
> select serialcolumn from table where oid = NNNN;
> since a single-row insert does return the OID of the inserted row.
> (Note this will be pretty slow if the table is big and does not have
> an index on OID.)
>
> Another possibility is to execute nextval() for yourself and then insert
> that result explicitly when you create the row.  Still takes two queries,
> though.

Another possibility is to "select currval('seqname')" where seqname is the
name of the sequence associated with the serial field.  currval's are
maintained per connection so there are no multiuser problems.  In many cases
you don't have retrieve the value but just use the expression is subsequent
statements.





view thread (13+ messages)  latest in thread

Message-ID: <000f01befde8$6ec81940$22d260d1@arcamax.com>
Permalink:  ../000f01befde8$6ec81940$22d260d1@arcamax.com/
Also on:    postgresql.org/message-id/000f01befde8$6ec81940$22d260d1@arcamax.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-interfaces@postgresql.org
  Cc: bryan@arcamax.com
  Subject: Re: [INTERFACES] ecpg and getting just assigned serial number
  In-Reply-To: <000f01befde8$6ec81940$22d260d1@arcamax.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