A demo for LastInsertId with MS SQL, please

Discussion of open issues, suggestions and bugs regarding UniDAC (Universal Data Access Components) for Delphi, C++Builder, Lazarus (and FPC)
Post Reply
Soida
Posts: 8
Joined: Sun 01 Aug 2010 05:35

A demo for LastInsertId with MS SQL, please

Post by Soida » Sat 15 Jan 2011 09:14

Hello!
I'm looking for a example uses LastInsertId with MS SQL, but it not found.
Please, a demo!!!
Thank you very much!!!

AndreyZ

Post by AndreyZ » Mon 17 Jan 2011 10:38

Hello,

The LastInsertId property can be used only with MySQL and PostgreSQL servers. For SQL Server you can use two approaches:
- if you use UniDAC SQL Generator, you should set the TCustomUniDataSet.SpecificOptions.QueryIdentity property to True (check that you have INDENTITY columns in the result set). In this case the identity field value will be set automatically.
- if you use your own SQL query, add this line to the query: SET :Identity_field = SCOPE_IDENTITY(). In this case you will get the field value as an output parameter.

Soida
Posts: 8
Joined: Sun 01 Aug 2010 05:35

Post by Soida » Tue 18 Jan 2011 02:59

Thank you very much!
But it not work!!!
Finally, i used STRORE PROCEDURE!!!

Dimon
Devart Team
Posts: 2910
Joined: Mon 05 Mar 2007 16:32

Post by Dimon » Wed 19 Jan 2011 08:54

If you use your own SQL query you should set ParamType of the ID parameter to ptInputOutput in the TCustomUniDataSet.BeforeUpdateExecute event handler, like this:

Code: Select all

  Params.ParamByName('ID').ParamType := ptInputOutput;

Post Reply