Articles → SQL SERVER → Update Column With Row Number In SQL Server

Update Column With Row Number In SQL Server






Table And Data




Picture showing the schema of the sample table




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




Picture showing the record inserted into the sample table



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




Picture showing the output of updating the column with row number in SQL server



Posted By  -  Karan Gupta
 
Posted On  -  Friday, November 27, 2015

Query/Feedback


Your Email Id  
 
Subject 
 
Query/FeedbackCharacters remaining 250