Thread: Getting the current value of a sequence

Getting the current value of a sequence

From
JORGE MALDONADO
Date:
I have been trying to get the current value of a sequence without success. I have read that "currval" does the job but I do not know how to use it. This is what I have tried and I get an error saying that "currval of sequence 'name_of_sequence' is not defined for the session.

SELECT currval('name_of_sequence')

I will very much appreciate your feedback.

Respectfully,
Jorge Maldonado

Re: Getting the current value of a sequence

From
hubert depesz lubaczewski
Date:
On Thu, Apr 23, 2015 at 06:17:42AM -0500, JORGE MALDONADO wrote:
> I have been trying to get the current value of a sequence without success.
> I have read that "currval" does the job but I do not know how to use it.
> This is what I have tried and I get an error saying that "currval of
> sequence 'name_of_sequence' is not defined for the session.
>
> SELECT currval('name_of_sequence')
> I will very much appreciate your feedback.

You can use currval() only after you used nextval() - in the same db
session.
Why do you want to use currval() before? What is the use case?

Best regards,

depesz

--
The best thing about modern society is how easy it is to avoid contact with it.
                                                             http://depesz.com/


Re: Getting the current value of a sequence

From
Roxanne Reid-Bennett
Date:
On 4/23/2015 7:17 AM, JORGE MALDONADO wrote:
> I have been trying to get the current value of a sequence without
> success. I have read that "currval" does the job but I do not know how
> to use it. This is what I have tried and I get an error saying that
> "currval of sequence 'name_of_sequence' is not defined for the session.
>
> SELECT currval('name_of_sequence')
>

Use Case can affect the right answer to meet your needs, so what you
plan on doing with that information is important for helping any of us
give you the "right" answer - but... the KISS answer is:

select last_value from name_of_sequence

Roxanne

--
[At other schools] I think the most common fault in general is to teach students how to pass exams instead of teaching
themthe science. 
Donald Knuth



Re: Getting the current value of a sequence

From
amulsul
Date:
Try following SQL:

SELECT last_value FROM name_of_sequence;





--
View this message in context:
http://postgresql.nabble.com/Getting-the-current-value-of-a-sequence-tp5846523p5846538.html
Sent from the PostgreSQL - novice mailing list archive at Nabble.com.