SQL Server Reset Identity Increment for all tables

前端 未结 9 1621
面向向阳花
面向向阳花 2021-01-30 05:38

Basically I need to reset Identity Increment for all tables to its original. Here I tried some code, but it fails.

http://pastebin.com/KSyvtK5b

Actual code from

9条回答
  •  無奈伤痛
    2021-01-30 06:21

    Be careful when using this command if your table contains data all your new inserts will result duplicate error

    exec sp_MSforeachtable @command1 = 'DBCC CHECKIDENT(''?'', RESEED,1)'
    

    to solve the problem you need to run this after that

    exec sp_MSforeachtable @command1 = 'DBCC CHECKIDENT(''?'', RESEED)'
    

    this will reset the seed to the last column identity if the data exists

提交回复
热议问题