I want to get the new created ID when you insert a new record in table.
I read this: http://msdn.microsoft.com/en-us/library/ms177564.aspx but it needs to create temporary table.
I want to return the ID after executing INSERT statement (assuming executing just one INSERT).
Example:
1 Joe Joe
2 Michael Mike
3 Zoe Zoe
When executing an INSERT statement, I want to return the created ID, means 4.
Can tell me how to do that using SQL statement or it is not possible ?
If your SQL Server table has a column of type
INT IDENTITY(orBIGINT IDENTITY), then you can get the latest inserted value using:This works as long as you haven’t inserted another row – it just returns the last
IDENTITYvalue handed out in this scope here.There are at least two more options –
@@IDENTITYandIDENT_CURRENT– read more about how they works and in what way they’re different (and might give you unexpected results) in this excellent blog post by Pinal Dave here.