SQL SERVER – Find Current Identity of Table

Many times we need to know what is the current identity of the column. I have found one of my developer using aggregated function MAX() to find the current identity.

USE AdventureWorks
GO
SELECT MAX(AddressID)
FROM Person.Address
GO

SQL Server query window displaying SELECT statement.

However, I prefer following DBCC command to figure out current identity.

USE AdventureWorks
GO
DBCC CHECKIDENT ('Person.Address', NORESEED)
GO 

SQL Server query for SQL Server2.

A Safer Way to Find the Current Identity Value

MAX() looks right, but it answers a different question. It returns the highest value in the table today, not the last value SQL Server handed out. Rows can be deleted, and inserts can fail or roll back, and each of those still uses up an identity value. So MAX() can be lower than the real counter, and the next row may get a number you did not expect.

DBCC CHECKIDENT is better, and notice the NORESEED option in the script above. Without that option, if the stored counter is lower than the highest value in the column, the command resets the counter. Most of the time you only want to look, not change anything, and NORESEED makes sure of that.

There are a few other options, and each answers a slightly different question:

  • IDENT_CURRENT('Person.Address') returns the last identity value generated for that table, from any session.
  • SCOPE_IDENTITY() returns the last value your own session inserted in the current scope. Use it right after an INSERT when you need the new ID.
  • @@IDENTITY also returns your session’s last value, but it can pick up a value from a trigger that inserts into another table, so I avoid it.
  • sys.identity_columns shows the seed, the increment and the last value for every identity column in the database.

When you do want to reset the counter, for example after you empty a test table, use DBCC CHECKIDENT with RESEED and a value. Be careful on tables that still have rows. A seed lower than the highest existing value leads to duplicate key errors, or to duplicate values if the column has no unique key.

One small warning: after a restart, SQL Server 2012 and later can jump the identity value ahead, because values are cached. Gaps are normal, so never build logic that expects identity numbers to be continuous.

Published by Pinal Dave on SQLAuthority. More of my work at pinaldave.com.

SQL Index, SQL Scripts, SQL Server DBCC
Previous Post
Comparing Two Databases Without a Scoreboard
Next Post
SQL SERVER – Create Check Constraint on Column

Related Posts

9 Comments. Leave new

  • hi,

    i tried it but it gives me error.
    but when i am just giving the table name it gives the correct result

    i think it should be like this

    USE Name_Of_The_DB
    GO
    DBCC CHECKIDENT (‘Name_Of_The_Table’)
    GO

    please check as when i given the column name it gives me error

    What is Person.Address???
    i didnot get it plz help………..

    Reply
  • even SELECT IDENT_CURRENT(‘table name’) gives the desired results

    Reply
  • or we can use @@identity

    Reply
  • @@Identity will not work. it will give the the last identity inserted in a transaction. i ll go with krishna.

    Reply
  • @@Identity should never be used. Even IDENT_CURRENT() is not recommended, since other transactions can insert rows in the meantime. Use SCOPE_IDENTITY() to retrieve identity value of a row just inserted. If there is more than one row, use SCOPE_IDENTITY() in compination with OUTPUT clause to collect all ID’s. Using sequence objects in Denali is another possible option. Here is complete example for OUTPUT clause:

    DROP TABLE t
    CREATE TABLE t(x int IDENTITY(1,1), y int)

    DECLARE @TableOfInsertedIds TABLE ( id int )

    INSERT INTO t( y ) — inserts into “t”
    OUTPUT INSERTED.x INTO @TableOfInsertedIds( id ) — this populates our table variable
    SELECT TOP 10 object_id FROM sys.all_objects

    SELECT * FROM t
    SELECT * FROM @TableOfInsertedIds

    Reply
  • Hi Pinal

    this DBCC CHECKIDENT (‘Person.Address’) will give the following result

    Checking identity information: current identity value ’22’, current column value ’22’.
    DBCC execution completed. If DBCC printed error messages, contact your system administrator.

    and this can not be used in any query to get the output in any programmin language, so i think your above query is not worth.

    Reply
  • abdulhannanijaz
    January 8, 2016 10:48 am

    Hi
    Doesn’t the 2nd query also reset the identity if there is Gap between identity & Seed value?

    AS SAID on MSDN
    DBCC CHECKIDENT(‘Table Name’) or DBCC CHECKIDENT(‘TABLENAME , RESEED)

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.