Click here to Skip to main content
11,704,509 members (57,798 online)
Click here to Skip to main content

SQL Server 2012 Auto Identity Column Value Jump Issue

, 14 Oct 2013 CPOL 64K 19
Rate this:
Please Sign up or sign in to vote.
From SQL Server 2012 version, when SQL Server instance is restarted then its auto Identity column value is jumped based on identity column datatype.

Introduction

From SQL Server 2012 version, when SQL Server instance is restarted, then table's Identity value is jumped and the actual jumped value depends on identity column data type. If it is integer (int) data type, then jump value is 1000 and if big integer (bigint), then jump value is 10000. From our application point of view, this increment is not acceptable for all the business cases specially when the value shows to the client. This is the special case/issue ships with only SQL Server 2012 and older versions have no such issue.

Background

A few days ago, our QA Engineer claims that one of our table's identity column jumped 10000. That means the last identity value of that table was 2200 now it is 12001. In our business logic is like that the value shows to the client and it will not be accepted by the client. So we must solve the issue.

Using the Code

The first time, we all are surprised and confused as to how it is possible? We usually do not insert any value in identity column (insert value to identity column is possible). The identity value is maintained by SQL Server itself. One of our core team members started investigation the issue and found out the solution. Now, I want to elaborate the issue and solution that was found out by my colleague.

How to Reproduce That?

You need to setup SQL Server 2012 and create a test database. Then create a table with auto identity column:

create table MyTestTable(Id int Identity(1,1), Name varchar(255));

Now insert 2 rows there:

insert into MyTestTable(Name) values ('Mr.Tom');
insert into MyTestTable(Name) values ('Mr.Jackson'); 

You see the result:

SELECT Id, Name FROM MyTestTable; 

The result is as expected. Now just restart your SQL Server service. There are various ways in which you can do it. We did it from SQL Server management studio.

Now, insert another 2 rows to the same table again:

insert into MyTestTable(Name) values ('Mr.Tom2');
insert into MyTestTable(Name) values ('Mr.Jackson2');

Now see the result:

SELECT Id, Name FROM MyTestTable;

Now you see that after restarting the SQL Server 2012 instance, then identity value starts with 1002. It means it jumped 1000. Previously, I said that we also see if the data type of that identity column is bigint, then it will jump 10000.

Is it really a bug?

Microsoft declares it is a feature rather than a bug and in many scenarios it would be helpful. But in our case, it would not be acceptable because that number is shown to the client and the client will be surprised to see that new number after jump and the new number depends on how many times SQL Server is restarted. If it is not visible to the client, then it might be acceptable so that the number is used internally.

Solutions

If we are not interested in this so called feature, then we can do two things to stop that jump.

  • Using Sequence
  • Register -t272 to SQL Server Startup Parameter

Using Sequence

First, we need to remove Identity column from tables. Then create a sequence without cache feature and insert number from that sequence. The following is the code sample:

CREATE SEQUENCE Id_Sequence
    AS INT
    START WITH 1
    INCREMENT BY 1
    MINVALUE 0
    NO MAXVALUE
   NO CACHE
insert into MyTestTable values(NEXT VALUE FOR Id_Sequence, 'Mr.Tom'); 
insert into MyTestTable values(NEXT VALUE FOR Id_Sequence, 'Mr.Jackson'); 

Register -t272 to SQL Server Startup Parameter

Open SQLServer configuration manager from your server. Select SQL Server 2012 instance there right client and select Properties menu. You will find a tabbed dialog window. You select start up parameters tab from there and register -t272. Then restart SQL Server 2012 instance again and see the difference:

Startup Parater

Points of Interest

If too many tables contain identity column to your database and all contain existing values, then it is better to go for solution 2. Because it is a very simple solution and its scope is server wise. This means if you add SQL Server 2012 parameter -t272 there, then it will affect all your databases there. If you want to create a new database and you need auto generated number field, then you can use solution 1, that means use sequence value to a column instead of auto Identity value. There are so many articles you can find online about when you will use auto identity column when using sequence and advantages/disadvantages of each other. I hope you will read all those and take the appropriate decision.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

S. M. Ahasan Habib
IXORA Solution Ltd.
Bangladesh Bangladesh
Mostly I work with MS technologies (ASP.NET MVC, WPF, C#, SQL Server, SSRS, SharePoint, Entity Framework, MSTest, Enterprise Library, MEF, WCF, WebAPI, MS Excel, IIS).
Non MS technologies which I love and use (Resharper, NHiberNet, JQuery, AngularJS, KnockoutJS, NodeJS, Python, MSpec, RihnoMock, Crystal Report, Subversion, Crome)

You may also be interested in...

Comments and Discussions

 
QuestionThanks Pin
Reivaj81020-Aug-15 7:31
memberReivaj81020-Aug-15 7:31 
QuestionThankx Pin
manish_kumar_gupta21-Jul-15 19:14
membermanish_kumar_gupta21-Jul-15 19:14 
QuestionThanks Pin
elsonms27-Apr-15 6:53
memberelsonms27-Apr-15 6:53 
QuestionNothing changed. Pin
mtmutlu11-Mar-15 0:07
membermtmutlu11-Mar-15 0:07 
AnswerRe: Nothing changed. Pin
mtmutlu11-Mar-15 0:17
membermtmutlu11-Mar-15 0:17 
QuestionJump not found is table is not in use at restart. Pin
Member 980018729-Oct-14 19:37
memberMember 980018729-Oct-14 19:37 
AnswerRe: Jump not found is table is not in use at restart. Pin
S. M. Ahasan Habib2-Nov-14 18:55
memberS. M. Ahasan Habib2-Nov-14 18:55 
QuestionThanks! Pin
Member 933680928-Oct-14 11:15
memberMember 933680928-Oct-14 11:15 
AnswerRe: Thanks! Pin
S. M. Ahasan Habib28-Oct-14 16:44
memberS. M. Ahasan Habib28-Oct-14 16:44 
GeneralMy vote of 5 Pin
Vihang Shah17-Sep-14 0:12
memberVihang Shah17-Sep-14 0:12 
Questionwhere this is helpful Pin
Member 110533502-Sep-14 4:57
memberMember 110533502-Sep-14 4:57 
AnswerRe: where this is helpful Pin
S. M. Ahasan Habib28-Oct-14 16:46
memberS. M. Ahasan Habib28-Oct-14 16:46 
General+5 Pin
Amol_B13-Aug-14 2:01
professionalAmol_B13-Aug-14 2:01 
GeneralMy vote of 5 Pin
Humayun Kabir Mamun8-Jul-14 22:48
memberHumayun Kabir Mamun8-Jul-14 22:48 
Question-T272 or -t272, are there differences? Pin
mikele19597-Jul-14 3:58
membermikele19597-Jul-14 3:58 
AnswerRe: -T272 or -t272, are there differences? Pin
S. M. Ahasan Habib28-Oct-14 16:45
memberS. M. Ahasan Habib28-Oct-14 16:45 
GeneralMy vote of 5 Pin
Halil ibrahim Kalkan25-Jun-14 20:00
memberHalil ibrahim Kalkan25-Jun-14 20:00 
QuestionUseful scenarios Pin
James Portelli17-Jun-14 2:06
memberJames Portelli17-Jun-14 2:06 
AnswerRe: Useful scenarios Pin
S. M. Ahasan Habib28-Oct-14 16:47
memberS. M. Ahasan Habib28-Oct-14 16:47 
QuestionThanks Pin
Ali Ahmadi Kousha11-May-14 19:58
memberAli Ahmadi Kousha11-May-14 19:58 
AnswerRe: Thanks Pin
S. M. Ahasan Habib11-May-14 20:18
memberS. M. Ahasan Habib11-May-14 20:18 
Questionhow about sql azure? Pin
Member 821198423-Apr-14 21:41
memberMember 821198423-Apr-14 21:41 
AnswerRe: how about sql azure? Pin
S. M. Ahasan Habib23-Apr-14 23:55
memberS. M. Ahasan Habib23-Apr-14 23:55 
Questionthis article is interested Pin
sankmahesh14-Oct-13 18:54
membersankmahesh14-Oct-13 18:54 
AnswerRe: this article is interested Pin
S. M. Ahasan Habib14-Oct-13 21:08
memberS. M. Ahasan Habib14-Oct-13 21:08 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150819.1 | Last Updated 14 Oct 2013
Article Copyright 2013 by S. M. Ahasan Habib
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid