原文链接:http://blog.csdn.net/phoenix36999/article/details/53304126

首先排除数据回滚及增加删除等操作。

按照这篇文章SQL Server 2012 Auto Identity Column Value Jump Issue的方法,就可以解决问题了。

以下是内容:

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.

从SQL Server2012版本中,当SQL Server实例重新启动,则表的IDENTITY值跳起来,实际跳下值取决于标识列的数据类型。如果它是整数(INT)数据类型,然后跳到值是1000,如果大的整数(BIGINT),然后跳值为10000。从我们的应用程序的角度来看,这个增量在所有的商业案例中是无法接受的,特别是当值展示给客户端时。这是特殊情况,问题仅随SQL Server 2012,旧版本有没有这样的问题。

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  方法二:注册-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的原因及解决方法的更多相关文章

  1. SQLServer2012自增列值跳跃的问题

    2012引入的新特性,重启之后会出现值跳跃的问题,如: 解决的方案: 1.使用序列(Sequence),2012引入的和Oracle一样的特性. 2.更改SQLServer启动服务的启动参数,增加[- ...

  2. SQLSERVER 自增列,值突然增大1000

    SQLSERVER 自增列,值突然增大1000https://blog.csdn.net/lichxi1002/article/details/40074247  

  3. sql server 自增列,值突然增大1000的情况

    sql server 自增列,值突然增大1000的情况   解决方法: 1 打开配置管理器2左面点击sql服务3右面 右键点击SQL Server(MSSQLSERVER) 4点击 启动参数5 在参数 ...

  4. MySQL--REPLACE INTO更新自增列值引发的异常

    ##=====================================================================##测试环境:MySQL版本:MySQL 5.7.19复制 ...

  5. sql server自增列值的获取

    IDENT_CURRENT(tbname) 是看表对象.所以没有受作用域限制. SCOPE_IDENTITY()  受作用域限制.同一个会话里面不同作用域也会有差异 @@IDENTITY  受会话限制 ...

  6. mysql 清空或删除表数据后,控制表自增列值的方法

    http://blog.sina.com.cn/s/blog_68431a3b0100y04v.html 方法1: truncate table 你的表名 //这样不但将数据全部删除,而且重新定位自增 ...

  7. SQL Server 强行Insert包含自增列值的记录

    SET IDENTITY_INSERT 表 ON INSERT INTO 表 ([ID] ,[SequenceNumber] ,[EnumCode] ,[Description]) VALUES ( ...

  8. jquery ajax 返回值 中文时乱码或变成问号解决方法

    转载自jquery的 ajax返回值为中文时乱码解决方法 用jquery的ajax,遇到个问题,服务器端从数据库取到的数据没有出现中文乱码问题(日志打出来是没有乱码的),但是异步传到客户的时候却出现了 ...

  9. onchange监听input值变化及input隐藏后change事件不触发的原因与解决方法(设置readonly后onchange不起作用的解决方案)

    转自:https://www.cnblogs.com/white0710/p/7338456.html 1. onchange事件监听input值变化的使用方法: <input id=" ...

随机推荐

  1. OOP,WEB开发实用小技巧

    偶然读到一篇博客,记录一下心得.这种设计对于新手来说一般是想不到的,它充分的发挥了OOP语言的特性,让代码专用而清爽.这是不是重构的思想呢? 我们在写业务层的时候,有很多方法是重复功能的,我们就可以使 ...

  2. java 多线程 4 线程池

    系统启动一个新线程的成本是比较高的,因为它涉及到与操作系统的交互.在这种情况下,使用线程池可以很好的提供性能,尤其是当程序中需要创建大量生存期很短暂的线程时,更应该考虑使用线程池. 与数据库连接池类似 ...

  3. GridView利用PagerTemplate做分页显示设置上一页下一页转到下拉转页

    效果如图: 代码如下: aspx页: <asp:GridView ID="GridViewMain" runat="server" OnPageIndex ...

  4. 关于node的http模块

    var http = require('http'); /** * 创建服务器的两种写法,第一种写法如下 * 由于server已经继承了EventEmitter的事件功能,所以可以使用高级函数编写方式 ...

  5. HTC vive开发:关于手柄按键

    一.关于左右手柄的对应关系 两个手柄和SteamVR_TrackedObject.EIndex是对应的,一个是EIndex.Device2,另一个是EIndex.Device3(有编号的那个) 在场景 ...

  6. SQL 从指定表筛选指定行信息 获取表行数

    1.获取指定表的行数 --获取表中数据行数 --select max([列名]) from 表名 2.筛选指定表的指定行数据(数据表分页获取) http://www.cnblogs.com/morni ...

  7. Java 中文乱码问题总结

    开发java应用出现乱码是很常见的,毕竟现在unicode的使用还不是很广泛,在使用gb2312(包含了gbk简体,big5繁体)的系统中要正确 实现中文的display和数据库的存储是最基本的要求. ...

  8. 使用Grunt构建自动化开发环境

    1.准备工作 1)首页确保电脑上网,以及能够访问https://registry.npmjs.org/,因需从此网站中下载安装相应的插件; 2)电脑安装Node.js,Grunt及Grunt插件都是基 ...

  9. Python中模块安装文件的创建及使用

    在Python编程中,我们常常需要自己编写模块,当模块文件写好了,就需要创建安装文件,方便模块的发布. 此时,常用的方法,就是使用Python distutils(代表distribution uti ...

  10. 修改html页面文字选中样式

    ::selection { background-color: #31B0D5; color: #fff; text-shadow: 0 1px 0 rgba(0,0,0,.2); }