in the group today, I see someone asking SQL Server self-value-added reset issue (SQL identiy column value reset)
Gossip less, directly on the code:
Body:
--create table
--Create test Tableif not exists(Select * fromsysobjectswhereName= 'test_1') begin Create TableTest_1 (IDint Identity(1,1)Primary Key, Namenvarchar( -), CreateDatedatetime); EndElse begin Drop Tabletest_1; Create TableTest_1 (IDint Identity(1,1)Primary Key, Namenvarchar( -), CreateDatedatetime); EndGo
View Code
--insert test data into table
Declare @ID int;Select @ID = 1001; while(@ID < 1101) begin Insert intoTest_1Values('Name'+ CONVERT(nvarchar( -),@ID),GETDATE()); Select @ID = @ID + 1; End--Select data from Test tableSelect * fromtest_1;
View Code
--delete data
--Delete from test_1;
View Code
--reset identity
DBCC Checkident (test_1, Reseed,0)
View results after executing the INSERT statement again
--insert data into test tableDeclare @ID int;Select @ID = 1001; while(@ID < 1101) begin Insert intoTest_1Values('NameS'+ CONVERT(nvarchar( -),@ID),GETDATE());--PS: The second name value has changed Select @ID = @ID + 1; EndSelect * fromtest_1;
View Code
Concluding remarks: DBCC checkident (test_1, reseed,0)--Reset identity value
Reset Identity Column Value in SQL Server (Identity Reset)