Re: [GENERAL] get the previous assigned sequence value

From: Ed Loehr <ELOEHR(at)austin(dot)rr(dot)com>
To: Kevin Heflin <kheflin(at)shreve(dot)net>
Cc: "PGSQL-General (E-mail)" <pgsql-general(at)postgresql(dot)org>
Subject: Re: [GENERAL] get the previous assigned sequence value
Date: 1999-12-09 04:15:32
Message-ID: 384F2CE4.115503C@austin.rr.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Kevin Heflin wrote:

> I've been looking through the online docs, so far have not found this
> information.
>
> After an INSERT, I want to retrieve the value of the sequence I use for
> unique_ids
>
> Any suggestions ?

Use the OID returned from the INSERT to select the newly created value,
i.e.,

insert into mytable (stuff...) ...
select id from mytable where oid = NNNN...

How to get the OID in the first example varies depending on your interface
(perl/DBI, PL/pgsql, etc).

Alternatively, use the currval(mytable_id_seq) function within the same
session immediately after the insert.

Cheers.
Ed

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Bruce Momjian 1999-12-09 05:00:23 Re: pg_typemgr ; -) [was Re: [GENERAL] no primary key on self designed type]
Previous Message Thomas Drillich 1999-12-09 03:08:05 pg_typemgr ; -) [was Re: [GENERAL] no primary key on self designed type]