Thursday, 25 July 2013

Query to reset identity without deleting record from table

USE Test

set Is Identity to No in column properties

SELECT Id ,ROW_NUMBER() OVER (ORDER BY Id ) AS RowNumber
     from [tbl_City]

DECLARE your_table_cursor CURSOR FOR
    SELECT [Id], 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  [tbl_City]
        SET [Id] = @RowNumber
        WHERE [Id]  = @YourTableId

    FETCH NEXT FROM your_table_cursor
    INTO @YourTableId, @RowNumber
END


CLOSE your_table_cursor
DEALLOCATE your_table_cursor


set Is Identity to Yes in column properties


No comments:

Post a Comment