SQL SERVER – DBCC command to RESEED Table Identity Value – Reset Table Identity

DBCC CHECKIDENT can reseed (reset) the identity value of the table. For example, YourTable has 25 rows with 25 as last identity. If we want next record to have identity as 35 we need to run following T SQL script in Query Analyzer.

DBCC CHECKIDENT (yourtable, reseed, 34)

If table has to start with an identity of 1 with the next insert then the table should be reseeded with the identity to 0. If identity seed is set below values that currently are in table, it will violate the uniqueness constraint as soon as the values start to duplicate and will generate error.

Reference : Pinal Dave (https://blog.sqlauthority.com)

SQL Index, SQL Scripts, SQL Server DBCC, SQL Server Security
Previous Post
SQL SERVER – Union vs. Union All – Which is better for performance?
Next Post
SQL SERVER – Fix: Sqllib error: OLEDB Error encountered calling IDBInitialize::Initialize. hr = 0x80004005. SQLSTATE: 08001, Native Error: 17

Related Posts

85 Comments. Leave new

  • abdulhannanijaz
    March 31, 2016 11:28 am

    Hey Pinal I reseed my Identity to 1 Plus Max .. but after that it give me primary key violation error..Any idea why is it behaving like that even it does not have such id as record.

    Reply
  • I have table with 2.5 billion and all identity values are full. What are our next steps?

    Reply
  • ill leave this for someone that needs it but i had to reset the db, so i deleted all rows, and then reseeded them, it first checks to see if the table even has and identity column and then it reseeds it to the NEXT value, meaning if there is data, then it will not reseed to 1, but to the next value, this way you reset all tables to the next number. Of course this is for the NEXT number, meaning that if identity is 13, next will be 14. this is in case you deleted rows after a certain identity

    — Seeks tables with identity columns to reseed

    EXEC sp_MSforeachtable ‘IF EXISTS (SELECT * from syscolumns where id = isnull(object_id(”?”),0) and colstat & 1 = 1)
    BEGIN
    dbcc checkident(”?”,reseed,0)with NO_INFOMSGS
    dbcc checkident(”?”,reseed)with NO_INFOMSGS
    END

    EXEC sp_MSForEachTable @command1=’ALTER TABLE ? WITH CHECK CHECK CONSTRAINT ALL’

    Reply

Leave a Reply