Well, there's no direct query I can think of but the following might help -
First, you need a unified set of results (I'm using MySQL syntax for ease of illustration)
SELECT columnA AS id FROM tableA
UNION
SELECT columnB AS id FROM tableB
Then, you need the range of numbers to limit your search for a new number
SELECT min(id), max(id) FROM
(FirstResults)
If you pulled your first results and put them into an array, you could get away without the 2nd DB query.
From there, I would use your programming language's array functions to create another array with all the possibilities between the min and max and compare the first array (all used id's) to the second array (all possible id's). In PHP, there is array_diff() to find the items in one array that are not in the other array. Then, select the lowest value in the array of differences.
Depending on the number of combined records in the 2 tables and the frequency of your need to perform this search, there are other approaches (like iterating through all the possibilities until you found one not in the first result set).
Hope this gets you through your dilemma!
Hello all. I'm working on a project and I dont have enough ASP.NET/MS SQL experience to figure this one out.
I have 2 tables in an existing database, both with one column that contains an int value (lowest value of all tables is 11111):
TABLE A
11111
11114
11116
TABLE B
11112
11113
11117
I need to get the next available number between the two tables (the first number that is higher than 11111 and not in either table). Based on this data it would be 11115.
Is there a simple SQL statement I'm not thinking of? Can someone help me with this code or at least the logic of it? Thanks!
-Chris
Chris Cummings
Integrity Web Development
Chattanooga Web Service