How do you find the smallest unused number in a SQL Server column?
I am about to import a large number of manually recorded records from Excel into a SQL Server table. They all have a numeric ID (called document number), but they weren't assigned sequentially for reasons that no longer apply, meaning from now on when my web site records a new record, it needs to assign it the smallest possible document number (greater than zero) that has not already been taken.
Is there a way to do this through plain SQL or is this a problem for TSQL/code?
Thanks!
EDIT
Special thanks to WW for raising the issue of concurrency. Given that this is a web app, it is multi-threaded by definition and anyone faced with this same problem should consider either a code or DB level lock to prevent a conflict.
LINQ
FYI - this can be accomplished via LINQ with the following code:
var nums = new [] { 1,2,3,4,6,7,9,10};
int nextNewNum = (
from n in nums
where !nums.Select(nu => nu).Contains(n + 1)
orderby n
select n + 1
).First();
nextNewNum == 5
I know this answer is late but you can find the smallest unused number by using a recursive table expression:
I faced a similar problem and came up with this:
Here is a simple approach. It may no be fast. It will not find missing numbers at the beginning.
Find the first row where there does not exist a row with Id + 1
Edit:
To handle the special case where the lowest existing id is not 1, here is a ugly solution:
No mention of locking or concurrency in any of the answers so far.
Consider these two users adding a document at nearly the same time:-
You either need to: a) Handle that error and go around the loop again looking for the next available Id, OR b) Take a lock out at the start of the process so only 1 user is looking for Ids at a particular time