Articles → SQL SERVER → Update Column With Row Number In SQL Server
Update Column With Row Number In SQL Server
Table And Data
Declare @i as Int
Set
@i = 1 While @i <> 50 Begin Insert into Employee(Employee_Name)
Values
(
'Employee ' + cast(
@i as varchar(2)
)
)
Set
@i = @i + 1 End
Updating Row Number
UPDATE
y
SET
y.[Count] = x.[Row_Number]
FROM
Employee y
Inner Join (
SELECT
Id,
ROW_NUMBER() OVER (
ORDER BY
[Id]
) AS [Row_Number]
FROM
Employee
) x on y.Id = x.Id
Output