SQL Server Reset Identity Increment for all tables

前端 未结 9 1527
面向向阳花
面向向阳花 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:13

    To reseed ONLY tables with an identity column you can use the next script. It also makes use of sp_MSforeachtable but taking into account the correct tables.

    EXEC sp_MSforeachtable '
    IF (SELECT COUNT(1) 
        FROM INFORMATION_SCHEMA.TABLES 
        WHERE TABLE_TYPE = ''BASE TABLE'' 
        AND ''[''+ TABLE_SCHEMA + ''].['' + TABLE_NAME + '']'' = ''?'' 
        AND OBJECTPROPERTY(OBJECT_ID(TABLE_NAME), ''TableHasIdentity'') = 1) > 0 
    BEGIN
        DBCC CHECKIDENT (''?'', RESEED, 1)
    END'
    

提交回复
热议问题