Click here to Skip to main content
12,633,764 members (30,408 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

24.7K views
11 bookmarked
Posted

6 Different Ways To Get The Current Identity Value in SQL

, 5 Nov 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
Identity Value
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
SELECT @@IDENTITY

DBCC CHECKIDENT
DBCC CHECKIDENT (TestOne, NORESEED)

MAX FUNCTION
SELECT MAX(id) FROM TestOne

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

IDENT_CURRENT
SELECT IDENT_CURRENT('TestOne')

SCOPE_IDENTITY
SELECT SCOPE_IDENTITY()

License

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

Share

About the Author

Ravi Tuvar
Technical Lead IntelliMedia Networks, Inc
India India
No Biography provided

You may also be interested in...

Pro
Pro

Comments and Discussions

 
Questionanother one way to get current identity value of the particular table Pin
Member 1268930716-Aug-16 22:11
memberMember 1268930716-Aug-16 22: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.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.161208.2 | Last Updated 5 Nov 2011
Article Copyright 2011 by Ravi Tuvar
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid