简体   繁体   English

如何在现有数据上重新设置/替换标识列

[英]How to reseed/replace identity columns on existing data

Hey all I want to reseed my IDENTITY COLUMN values starting from 1 I know how to do this with DBCC CHECKIDENT, however, I would like to replace the value in all existing rows.. This table has a little over 2 million rows. 嘿所有我想从1开始重新设置我的IDENTITY COLUMN值我知道如何用DBCC CHECKIDENT执行此操作,但是,我想替换所有现有行中的值。此表有超过200万行。

What is the best approach for this task? 这项任务的最佳方法是什么?

You can simply add a new identity column, a la How to add a new identity column to a table in SQL Server? 您可以简单地添加一个新的标识列, 如何在SQL Server中的表中添加新的标识列? . Just delete the old column and re-add it. 只需删除旧列并重新添加即可。 This will break any foreign keys, of course, but I assume since you are re-numbering everything I am guessing that's ok. 当然,这会破坏任何外键,但我认为,因为你正在重新编号我猜的一切都没关系。

See this example how to replace values, but RESEED is something else: 请参阅此示例如何替换值,但RESEED是其他内容:

CREATE TABLE t (id INT IDENTITY)
GO
INSERT t DEFAULT VALUES
GO 25

SET IDENTITY_INSERT t ON

delete t 

OUTPUT DELETED.Id+100 INTO T(Id)

SET IDENTITY_INSERT t Off

SELECT * FROM t

DROP TABLE t

An example of reseed : 重新种子的一个例子:

DBCC CHECKIDENT('YourTableName', 150, reseed)

AND

If you have to replace the value - with 2M rows it definitely have to take time 如果你必须更换值 - 用2M行,它肯定需要时间

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM