If you use a SQL server, you can make a recursive query that will connect to root.number = leaf.number + 1
If you select a number from the root and from the last recursion, and the recursion level should have a work request.
I would first test this performance, and then, if not satisfactory, let's move on to the cursor / line approach (which in this case will complete the task with one full scan, where the recursion may fail, reaching the depth of the recursion depth).
Otherwise, your parameters should store data in different ways and maintain a list of minimum, maximum numbers associated with the table.
This can actually be implemented in triggers with a not so high penalty for single-line updates (updates in one row of the base table will either update, delete, or split the row in the min-max table, this can be determined by querying only the "previous" and "next" lines.
Unreason
source share