Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Defensive Database Programming Expand / Collapse
Author
Message
Posted Wednesday, February 24, 2010 9:13 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Monday, January 06, 2014 6:58 PM
Points: 219, Visits: 823
Comments posted to this topic are about the item Defensive Database Programming
Post #871993
Posted Wednesday, December 04, 2013 7:44 AM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: 2 days ago @ 1:10 PM
Points: 56, Visits: 190
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.
Post #1519638
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse