I have the need to find the next available number in a set:
select min([pivot])
from [pivot]
where not exists (
select null as nothing
from product
where product.id = [pivot].[pivot])
However, this app will be used for a very long time and the [pivot] field is an integer. I don’t really want to create 2,147,483,647 [pivot] records (sequential numbers from zero to a big number in a table). Creating a view takes too long.
Is there some function in T-SQL (Microsoft SQL Server 2005 / 2008) which can provide a [pivot] table without actually creating one. Creating a [pivot] view is bad because it takes a lot of time to access the view.
See if something like this will work for you. In my example, #Test has a hole at 5 that should be returned, the second (#Test2) has no holes, so we expect a new ID to be returned. It’s done by self-joining on itself. I’m not sure why you’ve got a pivot there, so I may be misunderstanding your problem.