Re: Replacing IDENTITY with lastval() - Mailing list pgsql-odbc

From Andreas
Subject Re: Replacing IDENTITY with lastval()
Date
Msg-id 463CB520.90208@gmx.net
Whole thread Raw
In response to Replacing IDENTITY with lastval()  (Dmitry Samokhin <sdld@mail.ru>)
List pgsql-odbc
So - generally speaking - it is not advised to use lastval() at all?
At least not, if one is not 110% sure what happens in the application
now and on later modifications of the client application.

Thats a pitty, as I have only known lastval() for about 15 minutes when
I looked in the documentation for the sequence stuff.

On the other hand I ran today in troubles with sequences when I was
doing a bit coding on a migration from MS-Access DAO + JET to DAO, ODBC
+ PG.
With pure Access + DAO one can fetch the newly assigned AUTOVALUE by
simply reading it out of the newly created record right after AddNew and
before Update.

With ODBC + PG. as backend the SERIAL is undefined between AddNew and
Update.
And the recordset points not to the new record after Update but to the
first record in the recordset.
I had to use PG's CurrVal respectivly LastVal which would be easier
without having to know the name of the sequence.

I'm happy that I read your post before using LastVal all over the
place.   :)


Dmitry Samokhin schrieb:
> Accordind to the release notes, "Use lastval() function to replace IDENTITY on 8.1 or later servers" was introduced
inrelease 8.2.0205. This may cause incorrect results: "<...> assumes that your database does not expect any triggers to
firewhen the INSERT is executed. If a trigger does fire and if that trigger adds another row to a table, the @@IDENTITY
globalvariable would be set to point to that new Identity value—not the one your INSERT generated. <...> work for
simplesituations, but not when your database gets more sophisticated". 
> See "Managing an @@IDENTITY Crisis" on MSDN (http://msdn2.microsoft.com/en-us/library/ms971502.aspx) for more
details.
>
> The PostgreSQL documentation states that the lastval() function returns the value most recently returned by nextval
inthe current session. It works the same way as the @@IDENTITY variable in MSSQL. The currval(...) function returns a
valueof the explicitly specified sequence, this is exactly what we need. 
>
> Please consider to revert the code.
>
> Regards,
> Dmitry.
>
> ---------------------------(end of broadcast)---------------------------
> TIP 2: Don't 'kill -9' the postmaster
>
>


pgsql-odbc by date:

Previous
From: Hiroshi Inoue
Date:
Subject: Re: Replacing IDENTITY with lastval()
Next
From:
Date:
Subject: [ psqlodbc-Bugs-1003103 ] copy_and_convert_field(...) destroys bind info for wide char varchar columns