Usage of IDENT_CURRENT() in SQL Server

Bandi
Posted by Bandi under Sql Server category on | Points: 40 | Views : 363
If you want to know last auto incremented value or last identity value or recent ID in a table at any point of time ( means NOT only after INSERT statement) and not only in current session, use IDENT_CURRENT() fucntion.. It is specific to an object level. It will return the last ID of a table.

--Table Creation
CREATE TABLE Table1 ( ID int identity( 1,1), Name varchar(100))
GO
CREATE TABLE Table2 ( ID int identity( 10, 1), Name VARCHAR(100))
GO


--Data population and selscting IDENT_CURRENT value for each table...
INSERT Table1 VALUES( 'First Insert statemnt-Table1')
INSERT Table2 VALUES( 'LAST Insert statemnt-Table2')

SELECT ID Table1ID FROM Table1 -- output is 1
SELECT ID Table2ID FROM Table2 -- output is 10

SELECT IDENT_CURRENT('Table1') Table1IDUsingIdentCurrent -- output is 1
SELECT IDENT_CURRENT('Table2') Table2IDUsingIdentCurrent -- output is 10


Output:


--Clean up code
DROP TABLE Table1
DROP TABLE Table2


So IDENT_CURRENT() retuns identity value with respect to the table.....
It returns the last identity value generated for a specific table in any session and any scope

Comments or Responses

Login to post response