I have one table in SQL Server with Id, Plan_Id, sequence_no
and the following data.
Id Plan_Id sequence_no
----------- ------------ -
The simplest solution is to not use int, but decimal. That way, you only need to know the two closest siblings of the record, and only need to change the value of the sequence_no to a number between them.
For instance, if you have the following records in your database:
Id Plan_Id sequance_no
----------- ------------ -----------
1132507748 1167096719 1.0
1102670655 1167096719 2.0
1166210290 1167096719 3.0
1132507763 1167096719 4.0
1102670623 1167096719 5.0
1166210299 1167096719 6.0
and you want to move record 5.0 to between 1.0 and 2.0, all you need to do is to update it's sequence_no to 1.5:
Id Plan_Id sequance_no
----------- ------------ -----------
1132507748 1167096719 1.0
1102670623 1167096719 1.5
1102670655 1167096719 2.0
1166210290 1167096719 3.0
1132507763 1167096719 4.0
1166210299 1167096719 6.0
Decimal supports up to 38 digits so you can use up to 37 digits to the right of the decimal point, so even if you have a lot of changes in the order of the records, I'm pretty sure you will not have to worry about running out of slots.
If you want to display the order of the records in round numbers, you can always use row_number() over(order by sequence_no)
to get a nice int values display.