Click here to Skip to main content
15,881,882 members
Articles / Programming Languages / SQL
Tip/Trick

6 Different Ways To Get The Current Identity Value in SQL

Rate me:
Please Sign up or sign in to vote.
3.22/5 (7 votes)
5 Nov 2011CPOL 49.1K   11   1
Identity Value
SQL
CREATE TABLE TestOne (id INT IDENTITY,SomeDate DATETIME)
CREATE TABLE TestTwo (id INT IDENTITY,TestOneID INT,SomeDate DATETIME)

  INSERT TestOne VALUES(GETDATE())
  INSERT TestOne VALUES(GETDATE())
  INSERT TestOne VALUES(GETDATE())
  INSERT TestOne VALUES(GETDATE())


@@IDENTITY
SQL
SELECT @@IDENTITY

DBCC CHECKIDENT
SQL
DBCC CHECKIDENT (TestOne, NORESEED)

MAX FUNCTION
SQL
SELECT MAX(id) FROM TestOne

TOP 1 AND ORDER BY DESC
SQL
SELECT TOP 1 id FROM TestOne ORDER BY id DESC

IDENT_CURRENT
SQL
SELECT IDENT_CURRENT('TestOne')

SCOPE_IDENTITY
SQL
SELECT SCOPE_IDENTITY()

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Technical Lead IntelliMedia Networks, Inc
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Questionanother one way to get current identity value of the particular table Pin
Nandhaas16-Aug-16 21:11
Nandhaas16-Aug-16 21:11 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.