当前位置:Gxlcms > 数据库问题 > 转:SqlServer2012自增列值突然增大1000的原因及解决方法

转:SqlServer2012自增列值突然增大1000的原因及解决方法

时间:2021-07-01 10:21:17 帮助过:7人阅读

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  方法二:注册-t272到SQL Server启动参数

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:

打开SQLServer configuration manager。左边选择服务。右边在对应实例右键选择属性。点击启动参数。把-t272添加进去。重启sqlserver服务。再次新增数据进行观察。

技术图片

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.

如果您的数据库表包含太多的标识列,并且所有的表都包含现有的值,那么最好是去用解决方案2。因为它是一个非常简单的解决方案,它的范围是服务器。这意味着如果你添加SQL Server 2012参数- t272,然后它会影响你所有的数据库里。

如果你想创建一个新的数据库,你需要自动生成的数字字段,那么你可以使用解决方案1,这意味着使用序列值的列,而不是自动识别值。

有这么多的文章,你可以找到网上关于当你将使用自动识别列时,使用序列和优势/劣势的对方。我希望你会阅读所有这些,并采取适当的决定。

转:SqlServer2012自增列值突然增大1000的原因及解决方法

标签:back   sdn   rod   回滚   startup   cas   sqlserver   div   一个   

人气教程排行