Page 1 of 1

A demo for LastInsertId with MS SQL, please

Posted: Sat 15 Jan 2011 09:14
by Soida
Hello!
I'm looking for a example uses LastInsertId with MS SQL, but it not found.
Please, a demo!!!
Thank you very much!!!

Posted: Mon 17 Jan 2011 10:38
by AndreyZ
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.

Posted: Tue 18 Jan 2011 02:59
by Soida
Thank you very much!
But it not work!!!
Finally, i used STRORE PROCEDURE!!!

Posted: Wed 19 Jan 2011 08:54
by Dimon
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;