Обсуждение: RE: [GENERAL] getting the currval of a sequence

Поиск
Список
Период
Сортировка

RE: [GENERAL] getting the currval of a sequence

От
"Jackson, DeJuan"
Дата:
> >On Fri, 14 Aug 1998, Fran Fabrizio wrote:
> >
> > If you've recently done an insert and want to know what the value
> >was, you can do this:
> >
> > select last_value from mysequence;
>
> Alternatively, you can get your key from the sequence before you
> insert, and
> override the default value of the key (they will be the same only now
> you
> explicitly got it).  Now that you know what the key value use, after
> you
> insert it you can do anything you want to with it.  To do this just
> do:
>
>    select nextval ('sequencename');
>
> ...james
I think you should just go with what Vadim suggested and use:
    select currval('my_sequence');
This will be the same within a transaction, even if there is another
transaction using the same sequence.
But (if memory servers), you have to have had select a nextval from that
same sequence within the transaction.

Hope this clears things up.
        -DEJ


Re: [GENERAL] getting the currval of a sequence

От
James Olin Oden
Дата:


> I think you should just go with what Vadim suggested and use:
>         select currval('my_sequence');
> This will be the same within a transaction, even if there is another
> transaction using the same sequence.

Sorry, I wasn't thinking of it happening in the same transaction.  In that
case that would be the thing to do.  If on the other hand you needed that
number over multiple transactions, wouldn't what I had suggested work?

...james

>




Re: [GENERAL] getting the currval of a sequence

От
Vadim Mikheev
Дата:
Jackson, DeJuan wrote:
>
> I think you should just go with what Vadim suggested and use:
>         select currval('my_sequence');
> This will be the same within a transaction, even if there is another
                                 ^^^^^^^^^^^
                                 session

> transaction using the same sequence.
> But (if memory servers), you have to have had select a nextval from that
> same sequence within the transaction.
                           ^^^^^^^^^^^
                           session!

This is from create_sequence manual:

"...Function currval ('sequence_name') may be used
to  determine  number returned by last call to nextval for
specified sequence in current session."

Vadim