patternsqlCritical
Best way to get last identity inserted in a table
Viewed 0 times
lastidentitywayinsertedgettablebest
Problem
Which one is the best option to get the identity value I just generated via an insert? What is the impact of these statements in terms of performance?
SCOPE_IDENTITY()
- Aggregate function
MAX()
- SELECT
TOP 1IdentityColumn FROM TableNameORDER BY IdentityColumn DESC
Solution
Use
Result:
Use the
Result:
and why this is best faster option?
Performance aside, these are the only ones that are guaranteed to be correct in the default isolation level and/or with multiple users. Even if you ignore the correctness aspect, SQL Server holds the inserted value in
Ignoring the correctness aspect is like telling the mailman he did a good job delivering today's mail - he finished his route 10 minutes faster than his average time, the problem is, none of the mail was delivered to the right house.
Do not use any of the following:
These functions also fail whenever you insert two or more rows, and need all the identity values generated - your only option there is the
SCOPE_IDENTITY() if you are inserting a single row and want to retrieve the ID that was generated.CREATE TABLE #a(identity_column INT IDENTITY(1,1), x CHAR(1));
INSERT #a(x) VALUES('a');
SELECT SCOPE_IDENTITY();Result:
----
1Use the
OUTPUT clause if you are inserting multiple rows and need to retrieve the set of IDs that were generated.INSERT #a(x)
OUTPUT inserted.identity_column
VALUES('b'),('c');Result:
----
2
3and why this is best faster option?
Performance aside, these are the only ones that are guaranteed to be correct in the default isolation level and/or with multiple users. Even if you ignore the correctness aspect, SQL Server holds the inserted value in
SCOPE_IDENTITY() in memory, so naturally this will be faster than going and running your own isolated query against the table or against system tables. Ignoring the correctness aspect is like telling the mailman he did a good job delivering today's mail - he finished his route 10 minutes faster than his average time, the problem is, none of the mail was delivered to the right house.
Do not use any of the following:
@@IDENTITY- since this can't be used in all scenarios, for example when a table with an identity column has a trigger that also inserts into another table with its own identity column - you will get the wrong value back.
IDENT_CURRENT()- I go into detail about this here, and the comments are useful reading as well, but essentially, under concurrency, you will often get the wrong answer.
MAX()orTOP 1- you would have to protect the two statements with serializable isolation in order to ensure that theMAX()you get isn't someone else's. This is much more expensive than just usingSCOPE_IDENTITY().
These functions also fail whenever you insert two or more rows, and need all the identity values generated - your only option there is the
OUTPUT clause.Code Snippets
CREATE TABLE #a(identity_column INT IDENTITY(1,1), x CHAR(1));
INSERT #a(x) VALUES('a');
SELECT SCOPE_IDENTITY();INSERT #a(x)
OUTPUT inserted.identity_column
VALUES('b'),('c');Context
StackExchange Database Administrators Q#124847, answer score: 77
Revisions (0)
No revisions yet.