Thread: get last sequence

get last sequence

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



Re: get last sequence

From
Alfonso Peniche
Date:
"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.
> thanks!
> Chan

Select currval('sequence_name_id') from tablename;

this will give you the last sequence generated in tablename


Re: get last sequence

From
"Oliver Elphick"
Date:
"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