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)

Solarwinds
, , ,
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

  • can u explain pinal

    Reply
  • Kobus Fouche
    July 14, 2015 1:54 pm

    Good Day Dave,

    I have a dbo.dept table and it currently has 12190 rows. I read your blog and see I can run DBCC CHECKIDENT (yourtable, reseed, 34). My question would be what to insert in the place of 34 and will I keep the existing data ?

    Thank You

    Reply
    • if you reseed, same identity value would be generated again.

      set nocount on
      go
      create database foo
      go
      use foo
      go
      create table identity_table( i int identity(1,1), j varchar(10))
      go
      insert into identity_table (j) values (‘v1’)
      go
      insert into identity_table (j) values (‘v2’)
      go
      insert into identity_table (j) values (‘v3’)
      go
      insert into identity_table (j) values (‘v4’)
      go
      insert into identity_table (j) values (‘v5’)
      go

      Select * from identity_table

      delete from identity_table where i in (2,3)
      go
      Select * from identity_table
      go

      DBCC CHECKIDENT (identity_table, reseed, 1)

      /*
      Checking identity information: current identity value ‘5’.
      DBCC execution completed. If DBCC printed error messages, contact your system administrator.
      */

      insert into identity_table (j) values (‘v6’)
      go
      Select * from identity_table

      insert into identity_table (j) values (‘v7’)
      go

      insert into identity_table (j) values (‘v8’)
      go

      Select * from identity_table

      Reply
  • 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

Menu