Identity increment is jumping in SQL Server database

前端 未结 6 1347
夕颜
夕颜 2020-11-21 06:47

In one of my tables Fee in column \"ReceiptNo\" in SQL Server 2012 database identity increment suddenly started jumping to 100s instead of 1 depending on the fo

6条回答
  •  时光说笑
    2020-11-21 07:08

    I know my answer might be late to the party. But i have solved in another way by adding a start up stored procedure in SQL Server 2012.

    Create a following stored procedure in master DB.

    USE [master]
    GO
    SET ANSI_NULLS ON
    GO
    SET QUOTED_IDENTIFIER ON
    GO
    
    ALTER PROCEDURE [dbo].[ResetTableNameIdentityAfterRestart]
    AS
    BEGIN
    
    begin TRAN
        declare @id int = 0
        SELECT @id =  MAX(id) FROM [DatabaseName].dbo.[TableName]
        --print @id
        DBCC CHECKIDENT ('[DatabaseName].dbo.[TableName]', reseed, @id)
    Commit
    
    END
    

    Then add it in to Start up by using following syntax.

    EXEC sp_procoption 'ResetOrderIdentityAfterRestart', 'startup', 'on';
    

    This is a good idea if you have few tables. but if you have to do for many tables, this method still works but not a good idea.

提交回复
热议问题