• I noticed the examples in the book for creating a Numbers table. Of course they all work fine! I would like to contribute a variant that I happen to like. It depends on the (undocumented but heavily used) table master.dbo.spt_values, though any sufficiently-large table will do:

    SELECT TOP <quantity desired> IDENTITY(int, 0,1) AS n

    INTO #Numbers

    FROM master.dbo.spt_values v1, master.dbo.spt_values v2;

    ALTER TABLE #Numbers ADD PRIMARY KEY CLUSTERED (n);

    SELECT COUNT(*) from #Numbers;

    This gave me 5503716 natural numbers to use on my SQL Server 2005 instance and 6290064 on my SQL Server 2008 R2 instance. I believe that it is also much faster than the while-loop approach. Since the table is populated using a cartesian product, one can rapidly build tables of any size, subject to disk space limitations of course. You can fill in <quantity desired> with the upper limit of the range you need.