6 Different Ways To Get The Current Identity Value
From Wiki
This tip will show you how to get the current identity value from a table and also some things that might act a little different than you would expect.
Let's first create our two simple tables
- CREATE TABLE TestOne ( id INT IDENTITY ,SomeDate DATETIME )
- CREATE TABLE TestTwo ( id INT IDENTITY ,TestOneID INT ,SomeDate DATETIME )
- --Let's insert 4 rows into the table
- INSERT TestOne VALUES ( GETDATE ( ) )
- INSERT TestOne VALUES ( GETDATE ( ) )
- INSERT TestOne VALUES ( GETDATE ( ) )
- INSERT TestOne VALUES ( GETDATE ( ) )
Here are 6 ways to check for the current value
Contents [Hide ] |
@@IDENTITY
- SELECT @@IDENTITY
- --this returns 4
DBCC CHECKIDENT
- DBCC CHECKIDENT ( TestOne, NORESEED)
After running DBCC CHECKIDENT the message returned is Checking identity information: current identity value '4', current column value '4'. DBCC execution completed. If DBCC printed error messages, contact your system administrator.
MAX function
- SELECT MAX ( id)
- FROM TestOne
You can also check with the MAX function but this is not recommended because you might get some other identity value that is not yours but from a different user
TOP 1 and ORDER BY DESC
- SELECT TOP 1 id
- FROM TestOne
- ORDER BY id DESC
The same applies here as for the max function, this is not recommended
IDENT_CURRENT
- SELECT IDENT_CURRENT ( 'TestOne' )
SCOPE_IDENTITY
- SELECT SCOPE_IDENTITY ( )
This one is very similar to @@IDENTITY with one BIG difference (shown later)
Let's add a trigger to the TestOne table
- CREATE TRIGGER trTestOne ON [ dbo] .[ TestOne]
- FOR INSERT
- AS
- DECLARE @CreditUserID INT
- SELECT @CreditUserID = ( SELECT ID FROM Inserted)
- INSERT TestTwo VALUES ( @CreditUserID,GETDATE ( ) )
- GO
Let's insert another row into the TestOne table
- INSERT TestOne VALUES ( GETDATE ( ) )
Now run this
- SELECT @@IDENTITY --1
- SELECT @@IDENTITY ,SCOPE_IDENTITY ( ) ,IDENT_CURRENT ( 'TestOne' ) --1,5,5
Now why doesn't @@IDENTITY return 5 but 1?
T his is because @@IDENTITY doesn't care about scope and returns the last identity value from all the statements , which in this case is from the code within the trigger trTestOne
note : from envykok
Since insert into TestOne will trigger insert into TestTwo, so the last identity is come from TestTwo
So the bottom line is this: Always use SCOPE_IDENTITY() unless you DO need the last identity value regradless of scope (for example you need to know the identity from the table insert inside the trigger)
But don't let the above test convince you that SCOPE_IDENTITY() and IDENT_CURRENT('TestOne') will always return the same value. If another user adds a record to TestOne after you inserted but before you run the code for IDENT_CURRENT('TestOne'),the value will be 6 instead of 5 for IDENT_CURRENT('TestOne').
This is because it returns the last identity value on the table no matter what user added the record.
note: from envykok
this is because IDENT_CURRENT('TestOne') is any session and any scope , so if other user in other session add record into TestOne before u check IDENT_CURRENT('TestOne') value, then you will not able to get a value what you expected.
SCOPE_IDENTITY() is current session and the current scope
- --Clean up this mess
- DROP TABLE TestOne,TestTwo