Retrieve last inserted ID DataSnap / SqlServer [duplicate]

1

I am using SqlServer with my Delphi XE10 DataSnap application, but when I do an insert I would like the server to return the id that was just inserted in the sqlserver's autoincrement field so that I could display or update the client screen .

    
asked by anonymous 02.08.2016 / 23:47

2 answers

0

Ailton I use a lot of these two options that sql server gives:

IDENT_CURRENT returns the last identity value generated for a specific table in any session and scope. But you need to ensure that your insert worked, otherwise it will pick up the last Identity from the table even without inserting anything.

Ex: SELECT IDENT_CURRENT ('KEY_NAME')

GO

SCOPE_IDENTITY returns the last generated Identity value for any table in the current session and scope. This is clear that you should also ensure the INSERT worked, the difference being that instead of looking directly at the table it looks at the current session that it was run, ie it will just get the Identity generated in that session.

Ex: SELECT SCOPE_IDENTITY ()

GO

The result of both options you can store in a variable and manipulate it any way you want.

    
10.08.2016 / 05:09
-1

In the function that you pass the client data to the server insert into the database, put "out variable" the out returns you the return of a given.

    
03.08.2016 / 15:17