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.
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
I would join the table to a subquery (not sure if you can do that in SQL Server) or create a view and join the table to that in the update:
CREATE VIEW InsurancesView AS
SELECT insuranceID, ROW_NUMBER() OVER ( ORDER BY RowNo ) AS newRowNo
FROM Insurances;
(assuming you have a primary key named insuranceID
)
UPDATE Insurances AS i
INNER JOIN InsurancesView AS iv
ON i.insuranceID = iv.insuranceID
SET i.RowNo = iv.newRowNo;
Hope this helps.
WITH cte AS
(
SELECT Id, ROW_NUMBER() OVER(ORDER BY Name ) RowNo FROM lu_Domain
)
UPDATE dbo.lu_Domain
SET SortOrder = cte.RowNo
FROM dbo.lu_Domain d
INNER JOIN cte ON cte.id = d.Id
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.