SQL 重新播种有效,但自动增量从 0 开始
我有一些表,其中有一个身份列,我正在尝试重新播种。重新播种有效(我认为),但是当将新数据项插入表中时,标识列从 0 开始。
我的重新播种代码是:
DBCC CHECKIDENT(MyTable, RESEED, 0)
表的标识规范是:
- Identity Increment = 1 个
- 身份种子 = 1
快速说明 我正在重新播种之前对表执行删除操作
,请帮忙
I have some table that have an identity column that I am trying to reseed. The reseeding works (I think) but when a new data item is inserted into the table the identity column starts at 0.
My code to reseed is:
DBCC CHECKIDENT(MyTable, RESEED, 0)
The Identity Specifications for the tables are:
- Identity Increment = 1
- Identity Seed = 1
QUICK NOTE I am performing a delete on the tables prior to reseeding
Please help
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
当种子初始化为 0 时,似乎“未初始化”或截断的表(意味着没有数据插入到表中)将从 0 开始。但是当数据已插入到表中并且使用删除来清除所有数据时表的行。重新播种到 0 只会使数据库的最后一个种子为 0,下一个种子为 1。
这就是复制该问题的示例:
It appears that "uninitialized" or truncated tables (meaning no data has ever been inserted into the table) when the seed is initialized to 0 will start at 0. But when data has been inserted into a table and a delete is used to clear all rows of the table. Reseeding to 0 will only leave the database's last seed at 0 with the next seed being 1.
That said here is an example replicating the issue:
我认为您正在寻找的是:
这仅在表已增加的情况下,即已添加行然后删除。下一个插入的行现在将增加 0。
如果表没有行,请使用:
DBCC CHECKIDENT(MyTable, RESEED, 0)
I think what you are looking for is:
This is only if the Table has been incremented, that is row had been added and then deleted. The next inserted row will now be incremented giving 0.
In the case the table didn't have rows, use:
DBCC CHECKIDENT(MyTable, RESEED, 0)
使用该命令,您可以告诉 IDENTITY 将自身设置回 0 作为其新种子。
它不会返回到原始定义 (
IDENTITY(1,1)
),而是返回到您在 DBCC 命令中指定为第三个参数的值。如果您想返回使用 1 作为种子值,请使用:
如果您想使用 100,请使用:
您使用
DBCC CHECKIDENT
定义和设置的值将成为第一个当您向该表中插入行时,用于IDENTITY
列的新值。当您查看 MSDN 联机丛书文档时,您可以看到:
因此,实际上是您定义了 IDENTITY 列的新值 - 如果您像在帖子中那样传入 0,那么它将是 0 - 毕竟这就是您所要求的......
Using that command, you're telling the IDENTITY to set itself back to 0 as its new seed.
It will not go back to the original definition (
IDENTITY(1,1)
), but to the value you specify as the third parameter in the DBCC command.If you want to go back to using a 1 as your seed value, use:
If you want to go to 100, use:
That value that you defined and set by using
DBCC CHECKIDENT
will then be the first new value used for theIDENTITY
column when you insert a row into that table.When you check the MSDN Books Online documentation, you can see:
So it's really you who defines the new value of the
IDENTITY
column - if you pass in 0 as you do in your post, it will be 0 - that's what you asked for, after all...