Reset Identity Column Value in SQL Server (Identity Reset)
Posted 251608027
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Reset Identity Column Value in SQL Server (Identity Reset)相关的知识,希望对你有一定的参考价值。
前言:今天在群里看到有人在问SQL Server自增值重置问题(sqlserver identiy column value reset )
闲话少说,直接上代码:
正文:
--create table
--create test table if not exists(select * from sysobjects where name = \'Test_1\') begin create table Test_1 (ID int identity(1,1) primary key,Name nvarchar(128),CreateDate datetime); end else begin drop table Test_1; create table Test_1 (ID int identity(1,1) primary key,Name nvarchar(128),CreateDate datetime); end go
--insert test data into table
declare @ID int; select @ID = 1001; while(@ID < 1101) begin insert into Test_1 values(\'Name\'+ CONVERT(nvarchar(128),@ID),GETDATE()); select @ID = @ID + 1; end --select data from test table select * from Test_1;
--delete data
--delete data from test table delete from Test_1;
--reset identity
DBCC CHECKIDENT (Test_1, RESEED,0)
再次执行插入语句后查看结果
--insert data into test table declare @ID int; select @ID = 1001; while(@ID < 1101) begin insert into Test_1 values(\'NameS\'+ CONVERT(nvarchar(128),@ID),GETDATE());--PS:第二次Name值有改动 select @ID = @ID + 1; end select * from Test_1;
结束语:DBCC CHECKIDENT (Test_1, RESEED,0) -- reset identity value
以上是关于Reset Identity Column Value in SQL Server (Identity Reset)的主要内容,如果未能解决你的问题,请参考以下文章
ORA-00904: "IDENTITY_COLUMN": 使用 INSPECTDB DJANGO 的标识符无效错误