SQL Server Reset Identity Increment for all tables

前端 未结 9 1523
面向向阳花
面向向阳花 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'
    
    0 讨论(0)
  • 2021-01-30 06:15

    Use the below code,

    CREATE TABLE #tmptable
    (
        [seednvalue] int not null,
        [tablename] [nvarchar] (100) NULL
    ) 
    
    
    declare @seedvalue AS INT
    DECLARE @tablename AS VARCHAR(100)
    
    Declare #tablesIdentityCursor CURSOR
        for 
        SELECT 
        IDENT_CURRENT(TABLE_NAME)+1 AS Current_Identity,
        TABLE_NAME
        FROM INFORMATION_SCHEMA.TABLES
        WHERE OBJECTPROPERTY(OBJECT_ID(TABLE_NAME), 'TableHasIdentity') = 1
        AND TABLE_TYPE = 'BASE TABLE'  --AND TABLE_NAME='test11'
    
    delete from #tmptable
    Open #tablesIdentityCursor
    FETCH NEXT FROM #tablesIdentityCursor into @seedvalue, @tablename
    WHILE @@FETCH_STATUS = 0 BEGIN
    
        Insert into #tmptable Select @seedvalue , @tablename   
        DBCC CHECKIDENT (@tablename, reseed, @seedvalue) 
        FETCH NEXT FROM #tablesIdentityCursor into @seedvalue, @tablename
    END
    CLOSE #tablesIdentityCursor
    DEALLOCATE #tablesIdentityCursor
    SELECT * FROM #tmptable
    DROP TABLE #tmptable
    
    0 讨论(0)
  • 2021-01-30 06:19

    (I'm reposting my answer from this other SO page)

    Perhaps the easiest way (as crazy as this sounds and as code-smelly as it looks) is to just run DBCC CHECKIDENT twice like this:

    -- sets all the seeds to 1
    exec sp_MSforeachtable @command1 = 'DBCC CHECKIDENT (''?'', RESEED, 1)'
    
    -- run it again to get MSSQL to figure out the MAX/NEXT seed automatically
    exec sp_MSforeachtable @command1 = 'DBCC CHECKIDENT (''?'')'
    

    Done.

    If you want, you can run it once more to see what all the seeds were set to:

    -- run it again to display what the seeds are now set to
    exec sp_MSforeachtable @command1 = 'DBCC CHECKIDENT (''?'')'
    

    This is just a creative way to take advantage of the comment from the documentation:

    If the current identity value for a table is less than the maximum identity value stored in the identity column, it is reset using the maximum value in the identity column.

    0 讨论(0)
  • 2021-01-30 06:21

    Slight variation that handles Schemas a bit better...

    SELECT 
        IDENT_SEED(TABLE_SCHEMA+'.'+TABLE_NAME) AS Seed,
        IDENT_INCR(TABLE_SCHEMA+'.'+TABLE_NAME) AS Increment,
        IDENT_CURRENT(TABLE_SCHEMA+'.'+TABLE_NAME) AS Current_Identity,
        TABLE_SCHEMA+'.'+TABLE_NAME,
        'DBCC CHECKIDENT('''+TABLE_SCHEMA+'.'+TABLE_NAME+''', RESEED, '+CAST(IDENT_SEED(TABLE_SCHEMA+'.'+TABLE_NAME) AS VARCHAR(10))+')'
    FROM 
        INFORMATION_SCHEMA.TABLES
    WHERE 
        OBJECTPROPERTY(OBJECT_ID(TABLE_SCHEMA+'.'+TABLE_NAME), 'TableHasIdentity') = 1
    AND TABLE_TYPE = 'BASE TABLE'
    ORDER BY TABLE_SCHEMA, TABLE_NAME   
    
    0 讨论(0)
  • 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

    0 讨论(0)
  • 2021-01-30 06:22

    Slight tweak on marc_s answer.

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

    Those single-quotes around the ? character are important. That statement will cause SQL Server to automatically recalculate the next identity value for each table.

    0 讨论(0)
提交回复
热议问题