<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://sqlblog.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Geek City: Too Many Columns!</title><link>http://sqlblog.com/blogs/kalen_delaney/archive/2008/09/20/too-many-columns.aspx</link><description>As I am working on my new SQL Server 2008 internals book, I am finding many test situations in which I need to create a table with LOTS of columns. First of all, you're probably aware of the new SPARSE column feature that allows you to have up to 30,000</description><dc:language>en</dc:language><generator>CommunityServer 2.1 SP2 (Build: 61129.1)</generator><item><title>re: Geek City: Too Many Columns!</title><link>http://sqlblog.com/blogs/kalen_delaney/archive/2008/09/20/too-many-columns.aspx#9016</link><pubDate>Sun, 21 Sep 2008 07:57:21 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:9016</guid><dc:creator>IL</dc:creator><description>&lt;p&gt;It seems to be 1023 columns could be made ordinary and starting from 1024 have to belong sparse column set. I've modified last script like this:&lt;/p&gt;
&lt;p&gt;DECLARE @create varchar(max);&lt;/p&gt;
&lt;p&gt;DECLARE @tabname sysname;&lt;/p&gt;
&lt;p&gt;DECLARE @numcols int;&lt;/p&gt;
&lt;p&gt;DECLARE @col int;&lt;/p&gt;
&lt;p&gt;SELECT @numcols = 2000;&lt;/p&gt;
&lt;p&gt;SELECT @tabname = 'wide' + CONVERT(varchar, @numcols);&lt;/p&gt;
&lt;p&gt;SELECT @create = 'CREATE TABLE ' + @tabname +&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp;' (ID int IDENTITY, ';&lt;/p&gt;
&lt;p&gt;SELECT @col = 1;&lt;/p&gt;
&lt;p&gt;WHILE @col &amp;lt; @numcols BEGIN&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;IF (@col % 3) = 0&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; SELECT @create = @create + 'col' + CONVERT(varchar, @col) +&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; ' int';&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;IF (@col % 3) = 1 &amp;nbsp; &amp;nbsp;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; SELECT @create = @create + 'col' + CONVERT(varchar, @col) +&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; ' char(5)';&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;IF (@col % 3) = 2 &amp;nbsp; &amp;nbsp; &lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; SELECT @create = @create + 'col' + CONVERT(varchar, @col) +&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; ' varchar(25)';&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;IF @col &amp;gt; 1022 SELECT @create = @create + ' sparse,'&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;else SELECT @create = @create + &amp;nbsp;','&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;SELECT @col = @col + 1;&lt;/p&gt;
&lt;p&gt;END;&lt;/p&gt;
&lt;p&gt;SELECT @create = @create + 'col' + CONVERT(varchar, @col) +&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; ' xml column_set for all_sparse_columns);';&lt;/p&gt;
&lt;p&gt;PRINT @create &lt;/p&gt;
&lt;p&gt;EXECUTE (@create) &lt;/p&gt;
&lt;p&gt;EXEC ('INSERT INTO '+ @tabname + ' DEFAULT VALUES');&lt;/p&gt;
&lt;p&gt;EXEC ('SELECT * FROM '+ @tabname);&lt;/p&gt;
&lt;p&gt;No default values are allowed on sparse columns. Why?&lt;/p&gt;
</description></item><item><title>re: Geek City: Too Many Columns!</title><link>http://sqlblog.com/blogs/kalen_delaney/archive/2008/09/20/too-many-columns.aspx#9024</link><pubDate>Mon, 22 Sep 2008 05:30:52 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:9024</guid><dc:creator>Kalen Delaney</dc:creator><description>&lt;p&gt;Hi IL&lt;/p&gt;
&lt;p&gt;There is a default for sparse columns, it is NULL. The whole idea of sparse columns is that MOST of the rows will have a NULL value. If you want some other value to be the default and not have NULLs, that is not what sparse columns are for and you should re-evaluate your design. &lt;/p&gt;
&lt;p&gt;~Kalen&lt;/p&gt;
</description></item><item><title>re: Geek City: Too Many Columns!</title><link>http://sqlblog.com/blogs/kalen_delaney/archive/2008/09/20/too-many-columns.aspx#9029</link><pubDate>Mon, 22 Sep 2008 16:20:34 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:9029</guid><dc:creator>Bob</dc:creator><description>&lt;p&gt;I shudder to think what you're doing that would require more than 1,000 columns, much less 30,000!&lt;/p&gt;
</description></item><item><title>re: Geek City: Too Many Columns!</title><link>http://sqlblog.com/blogs/kalen_delaney/archive/2008/09/20/too-many-columns.aspx#9030</link><pubDate>Mon, 22 Sep 2008 16:26:38 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:9030</guid><dc:creator>Kalen Delaney</dc:creator><description>&lt;p&gt;I Bob... all I'm doing is creating a table! I always tell my students that 1000 (or 1024) columns is WAY too many. But since Microsoft allows very wide tables in 2008, my job is to tell people how it works. &lt;/p&gt;
&lt;p&gt;~Kalen&lt;/p&gt;
</description></item><item><title>re: Geek City: Too Many Columns!</title><link>http://sqlblog.com/blogs/kalen_delaney/archive/2008/09/20/too-many-columns.aspx#9031</link><pubDate>Mon, 22 Sep 2008 17:47:55 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:9031</guid><dc:creator>Kalen Delaney</dc:creator><description>&lt;p&gt;Bob.... &lt;/p&gt;
&lt;p&gt;I just realized I actually expressed my opinion of having so many columns in this interview at &lt;a rel="nofollow" target="_new" href="http://www.simple-talk.com/opinion/geek-of-the-week/kalen-delaney-geek-of-the-week/"&gt;http://www.simple-talk.com/opinion/geek-of-the-week/kalen-delaney-geek-of-the-week/&lt;/a&gt;...&lt;/p&gt;
&lt;p&gt;Also, the name of this post is TOO MANY Columns...&lt;/p&gt;
&lt;p&gt;:-)&lt;/p&gt;
&lt;p&gt;~Kalen&lt;/p&gt;
</description></item><item><title>re: Geek City: Too Many Columns!</title><link>http://sqlblog.com/blogs/kalen_delaney/archive/2008/09/20/too-many-columns.aspx#9441</link><pubDate>Fri, 10 Oct 2008 17:55:11 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:9441</guid><dc:creator>Mike</dc:creator><description>&lt;p&gt;While you might point to a 1000+ column table and say I can design that better using 10 columns and 2 or 3 tables, when it's a small cog in a much larger wheel of half closed and half open software that feed off the same table automatically, the 1000+ column table, on occasion, can sure come in handy for me.&lt;/p&gt;
</description></item><item><title>Geek City: Too Many Indexes!</title><link>http://sqlblog.com/blogs/kalen_delaney/archive/2008/09/20/too-many-columns.aspx#11245</link><pubDate>Sun, 18 Jan 2009 20:37:49 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:11245</guid><dc:creator>Kalen Delaney</dc:creator><description>&lt;p&gt;I wrote about &amp;quot;Too Many Columns&amp;quot; last September, and along with changes in SQL Server 2008 that allow&lt;/p&gt;
</description></item><item><title>re: Geek City: Too Many Columns!</title><link>http://sqlblog.com/blogs/kalen_delaney/archive/2008/09/20/too-many-columns.aspx#22061</link><pubDate>Tue, 09 Feb 2010 18:41:04 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:22061</guid><dc:creator>Adam</dc:creator><description>&lt;p&gt;Hi Kalen, it's strange how things happen. I am currently working with a client who has a mainframe system table which contains 6,000 columns, yes 6000! &lt;/p&gt;
&lt;p&gt;The requirement is to extract this data into SQL Server like for like. They are running SQL Server 2005 currently so I have split the data into 4 logical tables but they are moving to 2008 very soon so I wanted to know how viable this option would be? &lt;/p&gt;
&lt;p&gt;The vast majority of these fields actually contain data with no nulls would you still recommend using sparse columns for this solution, and would it work in the given scenario?&lt;/p&gt;
</description></item><item><title>re: Geek City: Too Many Columns!</title><link>http://sqlblog.com/blogs/kalen_delaney/archive/2008/09/20/too-many-columns.aspx#22571</link><pubDate>Tue, 23 Feb 2010 07:27:15 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:22571</guid><dc:creator>Adam</dc:creator><description>&lt;p&gt;Thanks Kalen,&lt;/p&gt;
&lt;p&gt;I thought I would come back with a quick update. The sparse columns option wasn't really viable as I wss reaching the row size limit in bytes before I got to the 6k columns. I stuck with the multiple table approach and this is working well.&lt;/p&gt;
&lt;p&gt;Adam&lt;/p&gt;
</description></item></channel></rss>