After you do the delete
, you can do an update:
with toupdate as (
select pi.*,
row_number() over (partition by productId order by [order]) as neworder
from ProductImages pi
)
update toupdate
set [order] = neworder;
The above updates the whole table. If you just want to update the changed product, add a where
clause:
with toupdate as (
select pi.*,
row_number() over (partition by productId order by [order]) as neworder
from ProductImages pi
where pi.ProductID = @ProductId
)
update toupdate
set [order] = neworder;