Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to assign ROW_NUMBER() to a column?

After reading this question, I have another similar question. Is there a simple way to get this SQL statement to work?

update [Insurances] set [RowNo]=ROW_NUMBER() over (order by [RowNo])

I have a RowNo column which I want to update whenever a record is deleted.

like image 474
iMan Biglari Avatar asked Dec 12 '22 22:12

iMan Biglari


1 Answers

UPDATE t1 
SET t1.RowNo = s.RowNo  
FROM  [Insurances] t1
INNER JOIN
(
     SELECT Id, ROW_NUMBER() OVER( ORDER BY Somefield DESC) RowNo
     FROM Insurances
) s ON t1.Id = s.Id
like image 143
Mahmoud Gamal Avatar answered Dec 28 '22 07:12

Mahmoud Gamal