Re: get last sequence - Mailing list pgsql-general

From Oliver Elphick
Subject Re: get last sequence
Date
Msg-id 200101271005.f0RA5LF23566@linda.lfix.co.uk
Whole thread Raw
In response to get last sequence  ("Mr. Chan" <daatmor@hongkong.com>)
List 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.Elphick@lfix.co.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



pgsql-general by date:

Previous
From: Marc SCHAEFER
Date:
Subject: Re: Re: Problem with inheritance
Next
From: "Oliver Elphick"
Date:
Subject: Re: Wild Cards