Hello!
I'm looking for a example uses LastInsertId with MS SQL, but it not found.
Please, a demo!!!
Thank you very much!!!
A demo for LastInsertId with MS SQL, please
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.
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.
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;