Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Generate list of all numbers (numbers not in use)

Author  Topic 

CF
Starting Member

4 Posts

Posted - 2007-02-21 : 09:05:31
I have an 'ID' column. I'm up to about ID number 40000, but not all are in use, so ID 4354 might not be in any row. I want a list of all numbers which aren't in use. I want to write something like this:

select [numbers from 0 to 40000] where <number> not in (select distinct id from mytable)


but don't know how. Any clues?

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2007-02-21 : 09:08:38
use LEFT JOIN

select n.NUMBER
from F_TABLE_NUMBER_RANGE(0, 40000) n left join mytable t
on n.NUMBER = t.ID
where t.ID is null


F_TALBE_NUMBER_RANGE can be found here http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=47685


KH

Go to Top of Page
   

- Advertisement -