T-SQL 101: 132 Identifying the Last Value Inserted with Identity Columns in SQL Server
I showed how identity columns are a special type of constraint. They can be int or bigint. Both work just fine. What you might need to know though, is the last value inserted automatically by SQL Server.
@@IDENTITY
For a long time, SQL Server only had a single option for this. It was the @@IDENTITY value.
The problem was that it could give you a value different to what you were looking for. The most common situation where this happened is if a trigger was set up. An INSERT trigger says when somebody does an insert, after it’s finished, execute this command as well. But what if that code in the trigger inserted a row somewhere else, perhaps to audit the inserts. The problem is that @@IDENTITY would return back the value from the second INSERT, not the one that you thought it was returning.
2025-03-20