[Lazarus] TSQLQuery: Getting autoincremented ID value after insert (MySQL)

silvioprog silvioprog at gmail.com
Sat Nov 15 01:41:27 CET 2014


On Fri, Nov 14, 2014 at 8:32 PM, Marco van de Voort <marcov at stack.nl> wrote:

> On Fri, Nov 14, 2014 at 03:11:30PM +0100, Michael Van Canneyt wrote:
> >
> > By the very nature of the problem: GetLastAutoIncValue simply cannot be
> implemented in general.
> >
> > In firebird, postgres,
>
> (and Oracle afaik)
>
> > these values are generated using a generator/sequence.
> > You don't know the name of the generator, and even if you did, it is
> impossible
> > to retrieve the value that was used to fill your particular record
> because the
> > sequence/generator may have been updated several thousands of times by
> the time
> > your second statement arrives.
>
> IIRC the currval from a sequence  is per session, so if you didn't insert
> again in the same transaction that is ok too.
>
> Moverover there is lastval to avoid knowing the sequence name
>
> from http://www.postgresql.org/docs/8.4/static/functions-sequence.html
>
> lastval
>
>     Return the value most recently returned by nextval in the current
> session. This function is identical to currval, except that instead of
> taking the sequence name as an argument it fetches the value of the last
> sequence used by nextval in the current session. It is an error to call
> lastval if nextval has not yet been called in the current session.


And PostgreSQL provides the RETURNING feature too. =)

--
Silvio Clécio
My public projects - github.com/silvioprog
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lazarus-ide.org/pipermail/lazarus/attachments/20141114/ae3eab92/attachment-0003.html>


More information about the Lazarus mailing list