删除后重置 SQL Server 中的自动增量

2021-11-30 00:00:00 sql-server auto-increment delete-row

我从 SQL Server 数据库的表中删除了一些记录.

I've deleted some records from a table in a SQL Server database.

表中的 ID 如下所示:

The IDs in the table look like this:

9910010112001201……

99 100 101 1200 1201...

我想删除后面的记录(ID > 1200),然后我想重置自动增量,这样下一个自动生成的 ID 将是 102.所以我的记录是连续的,有没有办法在 SQL Server 中做到这一点?

I want to delete the later records (IDs >1200), then I want to reset the auto increment so the next autogenerated ID will be 102. So my records are sequential, Is there a way to do this in SQL Server?

推荐答案

发出以下命令以重新播种 mytable 以从 1 开始:

Issue the following command to reseed mytable to start at 1:

DBCC CHECKIDENT (mytable, RESEED, 0)

在在线书籍(BOL、SQL 帮助)中阅读有关它的信息.还要注意您的记录不要高于您设置的种子.

Read about it in the Books on Line (BOL, SQL help). Also be careful that you don't have records higher than the seed you are setting.

相关文章