Recover last inserted Datasnap ID / Sqlserver

Asked

Viewed 1,764 times

1

I am using Sqlserver with my Delphi XE10 Datasnap application, but when I do an insert I would like the server to return me the id that was just entered in the auto increment (Identity) field of sqlserver so that I could show or update the client screen.

  • Your difficulty is getting the return value of the field identity or make the Datasnap function return its value?

2 answers

0

Ailton I use enough these two options that sql server gives us:

IDENT_CURRRENT 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 get the last Identity of the table even without inserting anything.

Ex: SELECT IDENT_CURRENT('NOME_TABELA')

GO

SCOPE_IDENTITY returns the last Identity value generated for any table in the current session and scope. This of course you should also ensure the INSERT worked, the difference is that instead of looking directly at the table it looks at the current session it was executed, IE, it will only 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 the way you want.

-1

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.