Hey guys,
doing an insert statement with the sqlDataAdapter. It adds a load of parameters to a table, in which SQL Server generates an id in an auto incremental field. How can I return the value of that field from the row that's just been added?
Thanks,
-
-
MySQL has a SELECT LAST_INSERT_ID() statement, I suspect SQL Server has something similar.
EDIT: Just checked it, and you can do SELECT @@IDENTITY, just create a SqlCommand for that and run with ExecuteScalar. -
Perfect!
thanks dude -
You're welcome.

-
Make sure you check out @@identity and ALSO SCOPE_IDENTITY as they return simpilar but not EXACTLY the same thing.
@@identity return the last number from a table that you inserted into, so if you have a trigger on the table, and it has an autonumber on it then it will have the value of the last table you inserted into, where as the SCOPE_IDENTITY() will return the last autonumber from the 'scope' of the stored procedure, etc. -
Also note that SELECT @IDENTITY is per connection, so if you do multiple insertions on the same connection, just the last identity is returned.
Thread Closed
This thread is kinda stale and has been closed but if you'd like to continue the conversation, please create a new thread in our Forums,
or Contact Us and let us know.