<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Scott Means &#187; sql</title>
	<atom:link href="http://smeans.com/tag/sql/feed/" rel="self" type="application/rss+xml" />
	<link>http://smeans.com</link>
	<description>Ripping the envelope of software development, one technology at a time.</description>
	<lastBuildDate>Tue, 06 Oct 2009 13:47:26 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>The mother of all dynamic SQL Crosstab View Builders.</title>
		<link>http://smeans.com/2009/02/03/the-mother-of-all-dynamic-sql-crosstab-view-builders/</link>
		<comments>http://smeans.com/2009/02/03/the-mother-of-all-dynamic-sql-crosstab-view-builders/#comments</comments>
		<pubDate>Tue, 03 Feb 2009 19:53:35 +0000</pubDate>
		<dc:creator>smeans</dc:creator>
				<category><![CDATA[coding]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[life]]></category>
		<category><![CDATA[crosstab]]></category>
		<category><![CDATA[sql]]></category>

		<guid isPermaLink="false">http://smeans.com/?p=110</guid>
		<description><![CDATA[Ok, so maybe it&#8217;s not too poetic, but it&#8217;s descriptive. As I found myself writing a dynamic crosstab-query builder in MS SQL for probably the fifth time of my career, I decided I&#8217;d post it here to save you all just a little time in your day. Make sure you use the saved time for [...]]]></description>
			<content:encoded><![CDATA[<p>Ok, so maybe it&#8217;s not too poetic, but it&#8217;s descriptive. As I found myself writing a dynamic crosstab-query builder in MS SQL for probably the fifth time of my career, I decided I&#8217;d post it here to save you all just a little time in your day. Make sure you use the saved time for something I&#8217;d approve of (smoking a good cigar, playing a little poker, something like that).</p>
<p>This proc is designed for when you have a dynamic set of row-oriented data you want to pivot and show column-wise. It takes four parameters:</p>
<p>@tableName &#8211; Name of the source table (or view) that will be queried by the new crosstab view.<br />
@excludeCols &#8211; A comma separated list of columns to exclude from the crosstab. In most cases, will be the ID column of the table.<br />
@valueColName &#8211; Name of the column in the @tableName table that contains the value in a given row.<br />
@targetViewName &#8211; Name of the view that the stored procedure will create. It will *automatically* drop this column before re-creating it, so be warned.</p>
<p>This proc also assumes that the @tableName table (or view) has a column called [colName]. It uses this value to create the corresponding columns across the top of the output view.</p>
<p>Good luck!</p>
<p><code>ALTER PROCEDURE BuildCrosstabView(<br />
	@tableName VARCHAR(100),<br />
	@excludeCols VARCHAR(1000),<br />
	@valueColName VARCHAR(1000),<br />
	@targetViewName VARCHAR(100)<br />
)<br />
AS<br />
BEGIN<br />
	-- SET NOCOUNT ON added to prevent extra result sets from<br />
	-- interfering with SELECT statements.<br />
	SET NOCOUNT ON;</p>
<p>	DECLARE @cols VARCHAR(4000)<br />
	SET @cols = ''</p>
<p>	EXEC ('SELECT DISTINCT colName INTO ##colList FROM ' + @tableName)<br />
	DECLARE col_cursor CURSOR FOR<br />
	SELECT colName<br />
	FROM ##colList</p>
<p>	OPEN col_cursor</p>
<p>	DECLARE @colName VARCHAR(100)</p>
<p>	FETCH NEXT FROM col_cursor INTO @colName</p>
<p>	WHILE @@FETCH_STATUS = 0<br />
	BEGIN</p>
<p>	IF @cols &lt;&gt; ''<br />
	BEGIN<br />
		SET @cols = @cols + ', '<br />
	END</p>
<p>	SET @cols = @cols + 'MAX(CASE colName WHEN ''' + @colName + ''' THEN ' + @valueColName + ' ELSE NULL END) AS [' + @colName + ']<br />
'<br />
	FETCH NEXT FROM col_cursor INTO @colName</p>
<p>	END</p>
<p>	CLOSE col_cursor<br />
	DEALLOCATE col_cursor</p>
<p>	DROP TABLE ##colList</p>
<p>	DECLARE @sql VARCHAR(3000)<br />
	SET @sql = 'SELECT DISTINCT ' + @excludeCols + ', ' + @cols + ' FROM ' + @tableName + ' GROUP BY ' + @excludeCols<br />
	PRINT @sql<br />
	EXECUTE (@sql)<br />
	IF EXISTS (SELECT * FROM sys.views WHERE object_id = OBJECT_ID(N'[dbo].[' + @targetViewName + ']'))<br />
		EXECUTE ('DROP VIEW ' + @targetViewName)<br />
	EXECUTE ('CREATE VIEW ' + @targetViewName + ' AS ' + @sql)<br />
END<br />
GO</code></p>
]]></content:encoded>
			<wfw:commentRss>http://smeans.com/2009/02/03/the-mother-of-all-dynamic-sql-crosstab-view-builders/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
