Re: get last sequence

From: "Oliver Elphick" <olly(at)lfix(dot)co(dot)uk>
To: "Mr(dot) Chan" <daatmor(at)hongkong(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: get last sequence
Date: 2001-01-27 10:05:21
Message-ID: 200101271005.f0RA5LF23566@linda.lfix.co.uk
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

"Mr. Chan" wrote:
>dear all,
> how to get last sequence from postgresql? I mean such mysql
>last_insert_id function or ms sql select @@identity.

I presume you mean get the last value assigned by the sequence.
If the sequence has been used in the current session (since connecting
to the backend) you can use the function currval('sequence_name').
this ignores any value issued to other users, so it is always safe
to use.

You cannot use currval if the sequence has not yet been used in the
session.

junk=# select currval('a_a_seq');
ERROR: a_a_seq.currval is not yet defined in this session

junk=# insert into a (b) values ('garbage');
INSERT 6821792 1

junk=# select currval('a_a_seq');
currval
---------
4
(1 row)

--
Oliver Elphick Oliver(dot)Elphick(at)lfix(dot)co(dot)uk
Isle of Wight http://www.lfix.co.uk/oliver
PGP: 1024R/32B8FAA1: 97 EA 1D 47 72 3F 28 47 6B 7E 39 CC 56 E4 C1 47
GPG: 1024D/3E1D0C1C: CA12 09E0 E8D5 8870 5839 932A 614D 4C34 3E1D 0C1C
========================================
"Come now, and let us reason together, saith the LORD;
though your sins be as scarlet, they shall be as white
as snow; though they be red like crimson, they shall
be as wool." Isaiah 1:18

Browse pgsql-general by date

  From Date Subject
Next Message Oliver Elphick 2001-01-27 10:16:16 Re: Wild Cards
Previous Message Marc SCHAEFER 2001-01-27 08:26:46 Re: Re: Problem with inheritance