I have two tables defined
TABNUMBERRANGES which defines a from-to definition of number which determines if a new organisation can be created that belongs to that number range, example
nFrom|nto|ngesnr
----------------
1 | 2 | 1
3 | 4 | 2
5 | 7 | 1
TABGESDEF which is a definition of organisations and what ges they belong to
ID|ngesnr
---------
1|1
2|1
3|2
4|2
5|1
Now what i want to achieve is to get the smallest possible number avaliable to use for TABGESDEF so that i alligns with the allowed ranges of TABNUMBERRANGES or to say that no space is avaliable and a new range has to be created in TABNUMBERRANGES, when creating a TABGESDEF record i know what gesdef i will assign it to, now what i need help with is going throught TABNUMBERRANGES for that ngesnr to get a avaliable number, is there any way I can do this using a SQL Query
In the given example i want to create a new TABGESDEF for ngesnr 1 and i should get 6 as my ID
1