2

I am having a table with one column only

id
--
1
2
3
5
5

I want to update the value of 4 th row to "4" so required output is :

id
--
1
2
3
4
5

how can i achieve this

4

1 回答 1

7
WITH CTE AS
(
    SELECT ID, RN = ROW_NUMBER() OVER (ORDER BY ID ASC)
    FROM dbo.TableName
)
UPDATE CTE SET ID = RN
WHERE ID <> RN

Demo

That will update all "incorrect" ID's according to their row number that is determined by the order.

于 2013-10-17T14:08:24.903 回答