Reset Identity Column Value in SQL Server (Identity Reset)

Source: Internet
Author: User
Tags getdate

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)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.