重置主键而不删除截断表 [英] Resetting Primary key without deleting truncating table
本文介绍了重置主键而不删除截断表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个带有主键的表,现在没有任何原因我不知道我什么时候插入数据它正在像这样加载
I have a table with a primary key, now without any reason I don't know when I am inserting data it is being loaded like this
Pk_Col Some_Other_Col
1 A
2 B
3 C
1002 D
1003 E
1901 F
有什么办法可以像下面那样重置我的表格,而不删除/截断表格?
Is there any way I can reset my table like below, without deleting/ truncating the table?
Pk_Col Some_Other_Col
1 A
2 B
3 C
4 D
5 E
6 F
推荐答案
您无法更新 IDENTITY 列,因此 DELETE/INSERT 是唯一的方法.您可以重新设定 IDENTITY 列并重新创建数据,如下所示:
You can't update the IDENTITY column so DELETE/INSERT is the only way. You can reseed the IDENTITY column and recreate the data, like this:
DBCC CHECKIDENT ('dbo.tbl',RESEED,0);
INSERT INTO dbo.tbl (Some_Other_Col)
SELECT Some_Other_Col
FROM (DELETE FROM tbl OUTPUT deleted.*) d;
假设没有引用此数据的外键.
That assumes there are no foreign keys referencing this data.
这篇关于重置主键而不删除截断表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文