THE SQL Server Blog Spot on the Web

Welcome to SQLblog.com - The SQL Server blog spot on the web Sign in | Join | Help
in Search

Adam Machanic

Adam Machanic, Boston-based independent database consultant, writer, and speaker, shares his experiences with programming, performance tuning, and optimizing SQL Server 2000, 2005, and 2008, in conjunction with related technologies such as .NET.

You REQUIRE a Numbers table!

Looking at my list of upcoming articles, I keep seeing the same theme repeated over and over. A sequence table of Numbers.

 

Numbers tables are truly invaluable. I use them all of the time for string manipulation, simulating window functions, populating test tables with lots of data, eliminating cursor logic, and many other tasks that would be incredibly difficult without them.

Is using a table of numbers a hack, as I've seen some people claim? No. Show me another way to efficiently do all of the things a numbers table can. Does it waste space? No. The script below will use up around 900 KB of disk space in each database. That's absolutely nothing. You'll end up getting millions, maybe billions of times the disk space investment back in terms of ease of development and time saved.

So henceforth, I will assume in this blog that everyone reading has a Numbers table. And I will politely link to this article as a gentle reminder. But I want you to open Query Analyzer right now and use the following script:

 

USE Model
GO

CREATE TABLE Numbers
(
Number INT NOT NULL,
CONSTRAINT PK_Numbers
PRIMARY KEY CLUSTERED (Number)
WITH FILLFACTOR = 100
)

INSERT INTO Numbers
SELECT
(a.Number * 256) + b.Number AS Number
FROM
(
SELECT number
FROM master..spt_values
WHERE
type = 'P'
AND number <= 255
) a (Number),
(
SELECT number
FROM master..spt_values
WHERE
type = 'P'
AND number <= 255
) b (Number)
GO

There. Now you automatically have a Numbers table in every database you create, populated with every number between 0 and 65535. That's big enough for most tasks. If you need more numbers, just insert more! It's fun and easy! And trust me, you'll use this table. A lot. And you'll thank me one day, probably by sending me lots of gifts, as a very small token of your appreciation.

But in the meantime, here are two links with more information on using a numbers table:

ASP FAQ #2516, "Why should I consider using an auxiliary numbers table?"

Fun with numbers in Transact-SQL queries

 


Update, December 1, 2005: Fixed the insert script for SQL Server 2005; master..spt_values now has a lot more numbers (0-2048 instead of 0-255) so as-is the script was failing. It should work properly now.



Published Wednesday, July 12, 2006 10:18 PM by Adam Machanic
Filed under:

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

 

BI Thoughts and Theories said:

A question that comes up occasionally is how to handle errors that occur on the OLE DB Destination in

September 5, 2008 3:43 AM
 

Problem in writing a sql query - Page 3 - dBforums said:

January 12, 2009 8:51 AM
 

Adam Machanic said:

There are many techniques for splitting a string in T-SQL (in other words, taking a character-delimited

January 25, 2009 2:44 PM
 

Adam Machanic said:

I have absolutely no idea why anyone wants to do this, but I keep answering the same question in forums:

January 25, 2009 2:44 PM
 

Adam Machanic said:

Continuing in my series of things you should probably not do in SQL Server but sometimes have to , I'm

January 25, 2009 2:45 PM
 

Adam Machanic said:

Ever want to see the text of a stored procedure, function, or trigger -- or manipulate the text in some

January 25, 2009 2:53 PM
 

Adam Machanic said:

Yes, another string splitting UDF from a guy who's obvioiusly become obsessed with TSQL string splitting.

January 25, 2009 2:54 PM
 

Creating multiple new records automatically. - dBforums said:

November 25, 2009 9:20 AM

Leave a Comment

(required) 
(optional)
(required) 
Submit

About Adam Machanic

Adam Machanic is a Boston-based independent database consultant, writer, and speaker. He has been involved in dozens of SQL Server implementations for both high-availability OLTP and large-scale data warehouse applications, and has optimized data access layer performance for several data-intensive applications. Adam has written for numerous web sites and magazines, including SQLblog, Simple Talk, Search SQL Server, SQL Server Professional, CoDe, and VSJ. He has also contributed to several books on SQL Server, including "Expert SQL Server 2005 Development" (Apress, 2007) and "Inside SQL Server 2005: Query Tuning and Optimization" (Microsoft Press, 2007). Adam regularly speaks at user groups, community events, and conferences on a variety of SQL Server and .NET-related topics. He is a Microsoft Most Valuable Professional (MVP) for SQL Server, a Microsoft Certified IT Professional (MCITP), and a member of the INETA North American Speakers Bureau.

This Blog

Syndication

News

Powered by Community Server (Commercial Edition), by Telligent Systems
  Privacy Statement