首页 > 代码库 > Reset Identity Column Value in SQL Server (Identity Reset)
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)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。