How do I replace in SQL an ID # of a bottom row to a certain ID # - I, and increment all the rest of the rows ID # with 1 starting with the row that carries this ID # -I?

This is what I tried, and simply got confused.

DECLARE @i int = 386
update tbl_ID 
    set ID = @i+1 where ID = @i 
    OFFSET (@Skip) ROWS FETCH NEXT (@Take) ROWS ONLY
    if ID = @i+1
    @i =  @i+1
WHILE @i < 388
BEGIN
    SET @i = @i + 1
    update tbl_ID 
    set ID  = @i where ID = @i - 1
    if ID  = @i
    OFFSET (@Skip) ROWS FETCH NEXT (@Take) ROWS ONLY    
END
1

There are 1 best solutions below

0
Yehuda On
update <table> 
set id = ID+2 where ID > 385 
update <table> set id = 386 where ID is NULL and <more info> 
update <table> set id = 387 where ID is NULL and <more info>