Click here to Skip to main content
Click here to Skip to main content

How to Reset Identity Column Value In SQL Server without Delete Records.

By , 17 Dec 2010
 
Remove foreign key reference (if any) from YourTabe and set Is Identity to No in column properties windows in Microsoft SQL Server Management Studio. Execute the bellow SQL using Management Studio.
 
USE YourDataBase
 
SELECT [YourTableId],ROW_NUMBER() OVER (ORDER BY YourTableId ) AS RowNumber
    into #tempTable from [YourTable]
 
DECLARE your_table_cursor CURSOR FOR
    SELECT [YourTableId], RowNumber
    FROM #tempTable
 
OPEN your_table_cursor
 
DECLARE @YourTableId int
DECLARE @RowNumber int
 
FETCH NEXT FROM your_table_cursor
INTO @YourTableId, @RowNumber
 
WHILE @@FETCH_STATUS = 0
BEGIN
    UPDATE  [YourTable]
        SET [YourTableId] = @RowNumber
        WHERE [YourTableId] = @YourTableId
 
    FETCH NEXT FROM your_table_cursor
    INTO @YourTableId, @RowNumber
END
 

CLOSE your_table_cursor
DEALLOCATE your_table_cursor
 
DROP TABLE #tempTable 
 
 
Set Is Identity to Yes and set Identity increment to 1 and Identity Seed to 1 in column properties windows in Management Studio. Then set reference.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Abdul Quader Mamun
Founder
Bangladesh Bangladesh
Member
I have been developing software/web application since 2002 mainly on Microsoft technologies. I am a MCSD and MCTS. I have developed a wide range of Web, Desktop and Mobile applications.

I am vast experience with Telerik technologies.

I am also experience with other technologies.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralReason for my vote of 2 See alternativememberEskern18 Dec '10 - 22:12 
Reason for my vote of 2
See alternative

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web01 | 2.6.130516.1 | Last Updated 17 Dec 2010
Article Copyright 2010 by Abdul Quader Mamun
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid