<?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>tim laqua dot com &#187; business intelligence</title>
	<atom:link href="http://timlaqua.com/tag/business-intelligence/feed/" rel="self" type="application/rss+xml" />
	<link>http://timlaqua.com</link>
	<description>Thoughts and Code from Tim Laqua</description>
	<lastBuildDate>Fri, 16 Mar 2012 16:48:05 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>Automating PowerPivot Data Refresh in Sharepoint 2010</title>
		<link>http://timlaqua.com/2012/03/automating-powerpivot-data-refresh-in-sharepoint-2010/</link>
		<comments>http://timlaqua.com/2012/03/automating-powerpivot-data-refresh-in-sharepoint-2010/#comments</comments>
		<pubDate>Fri, 16 Mar 2012 00:56:57 +0000</pubDate>
		<dc:creator>Tim</dc:creator>
				<category><![CDATA[Scripts & Code]]></category>
		<category><![CDATA[bi]]></category>
		<category><![CDATA[business intelligence]]></category>
		<category><![CDATA[sharepoint]]></category>

		<guid isPermaLink="false">http://timlaqua.com/?p=977</guid>
		<description><![CDATA[Of course we want to do this, it's a fundamental requirement. Dear Sharepoint, please refresh my PowerPivot when the data is ready, not on some arbitrary schedule. Until this functionality is built-in, people will continue to hack away at it to figure out how to make this happen. Is this supported? Certainly not - so [...]]]></description>
			<content:encoded><![CDATA[<p>Of course we want to do this, it's a fundamental requirement.  Dear Sharepoint, please refresh my PowerPivot when the data is ready, not on some arbitrary schedule.  Until this functionality is built-in, people will continue to hack away at it to figure out how to make this happen.  Is this supported?  Certainly not - so continue at your own risk.</p>
<p>So the method we'll be using here is just to mimic what Sharepoint does when you check the box to "Also refresh as soon as possible" in the schedule configuration page.  To accomplish this we open the profiler, connect it to whatever instance our Sharepoint PowerPivot database is hosted on and filter you TextData to "%Schedule%" or filter to just the Sharepoint PowerPivot database (SP2010_PowerPivot_Service_Application in our case) - then open up your test PowerPivot schedule configuration, check the box, click OK, wait for the schedule history to come back up and then stop the trace.  Now you know you've got what you need, you just have to find it:</p>
<p><a href="http://timlaqua.com/wp-content/uploads/2012/03/Profiler-Events.jpg"><img src="http://timlaqua.com/wp-content/uploads/2012/03/Profiler-Events.jpg" alt="" title="Profiler Events" width="797" height="56" class="alignnone size-full wp-image-978" /></a><br />
<span id="more-977"></span><br />
So here we have Sharepoint asking for information about the schedule (identified by the ItemID from DataRefresh.Items) and then passing the data it received to PersistSchedule.  If you take a close look at the PersistSchedule parameters, the last one is @RunNow = 1.  So clearly, this is what we're after.  We went the most tedious route and manually looked up the ItemID in DataRefresh.Items:</p>

<div class="wp_syntax"><div class="code"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">SET</span> <span style="color: #0000FF;">NOCOUNT</span> <span style="color: #0000FF;">ON</span>
&nbsp;
<span style="color: #0000FF;">DECLARE</span> @ItemID <span style="color: #0000FF;">UNIQUEIDENTIFIER</span> <span style="color: #808080;">=</span> <span style="color: #FF0000;">'30D39B0E-DA8B-489B-A713-3B29D208B51F'</span>
&nbsp;
<span style="color: #0000FF;">DECLARE</span>
	@ItemName <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">NVARCHAR</span><span style="color: #808080;">&#93;</span><span style="color: #808080;">&#40;</span><span style="color: #000;">200</span><span style="color: #808080;">&#41;</span>,
	@SPSiteID <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">UNIQUEIDENTIFIER</span><span style="color: #808080;">&#93;</span>,
	@SPWebID <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">UNIQUEIDENTIFIER</span><span style="color: #808080;">&#93;</span>,	
	@LastModifiedBy <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">NVARCHAR</span><span style="color: #808080;">&#93;</span><span style="color: #808080;">&#40;</span><span style="color: #000;">450</span><span style="color: #808080;">&#41;</span>,
	@ScheduleLastUpdatedBy <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">NVARCHAR</span><span style="color: #808080;">&#93;</span><span style="color: #808080;">&#40;</span><span style="color: #000;">450</span><span style="color: #808080;">&#41;</span>,
	@UserIdentity <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">NVARCHAR</span><span style="color: #808080;">&#93;</span><span style="color: #808080;">&#40;</span><span style="color: #000;">450</span><span style="color: #808080;">&#41;</span>,
	@EmailNotification <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">BIT</span><span style="color: #808080;">&#93;</span>, 
	@EmailList <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">NVARCHAR</span><span style="color: #808080;">&#93;</span><span style="color: #808080;">&#40;</span><span style="color: #000;">450</span><span style="color: #808080;">&#41;</span>,
	@SecurityConfiguration <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">INT</span><span style="color: #808080;">&#93;</span>,
	@SSApplicationID <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">NVARCHAR</span><span style="color: #808080;">&#93;</span><span style="color: #808080;">&#40;</span><span style="color: #000;">256</span><span style="color: #808080;">&#41;</span>,
	@ProcessAllDataSources <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">BIT</span><span style="color: #808080;">&#93;</span>,
	@RV <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">BIGINT</span><span style="color: #808080;">&#93;</span>,
	<span style="color: #008080;">-- Schedule info</span>
	@Enabled <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">BIT</span><span style="color: #808080;">&#93;</span>, 
	<span style="color: #008080;">-- Schedule details</span>
	@NextProcessDate <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">DATETIME</span><span style="color: #808080;">&#93;</span>,
	@ScheduleStartDate <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">DATETIME</span><span style="color: #808080;">&#93;</span>,
	@FrequencyKey <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">NCHAR</span><span style="color: #808080;">&#93;</span><span style="color: #808080;">&#40;</span><span style="color: #000;">1</span><span style="color: #808080;">&#41;</span>,
	@RepeatFrequency <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">INT</span><span style="color: #808080;">&#93;</span>,
	@Sunday <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">BIT</span><span style="color: #808080;">&#93;</span>,
	@Monday <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">BIT</span><span style="color: #808080;">&#93;</span>,
	@Tuesday <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">BIT</span><span style="color: #808080;">&#93;</span>,
	@Wednesday <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">BIT</span><span style="color: #808080;">&#93;</span>,
	@Thursday <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">BIT</span><span style="color: #808080;">&#93;</span>,
	@Friday <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">BIT</span><span style="color: #808080;">&#93;</span>,
	@Saturday <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">BIT</span><span style="color: #808080;">&#93;</span>,
	@Weekday <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">BIT</span><span style="color: #808080;">&#93;</span>, 
	@WeekendDay <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">BIT</span><span style="color: #808080;">&#93;</span>, 
	@<span style="color: #0000FF;">DAY</span> <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">BIT</span><span style="color: #808080;">&#93;</span>, 
	@MonthlyPeriod <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">TINYINT</span><span style="color: #808080;">&#93;</span>, 
	@MonthlySpecificDay <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">INT</span><span style="color: #808080;">&#93;</span>, 
	@ProcessAfterBusinessHours <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">BIT</span><span style="color: #808080;">&#93;</span>, 
	@SpecificTime <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">DATETIME</span><span style="color: #808080;">&#93;</span>,
	@RunNow <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">BIT</span><span style="color: #808080;">&#93;</span>
&nbsp;
<span style="color: #0000FF;">DECLARE</span> @Schedule <span style="color: #0000FF;">TABLE</span> <span style="color: #808080;">&#40;</span>
	<span style="color: #808080;">&#91;</span>ItemID<span style="color: #808080;">&#93;</span> <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">UNIQUEIDENTIFIER</span><span style="color: #808080;">&#93;</span> <span style="color: #808080;">NOT</span> <span style="color: #808080;">NULL</span>,
	<span style="color: #808080;">&#91;</span>Enabled<span style="color: #808080;">&#93;</span> <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">BIT</span><span style="color: #808080;">&#93;</span> <span style="color: #808080;">NOT</span> <span style="color: #808080;">NULL</span>,
	<span style="color: #808080;">&#91;</span>RV<span style="color: #808080;">&#93;</span> <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">BIGINT</span><span style="color: #808080;">&#93;</span> <span style="color: #808080;">NULL</span>,
	<span style="color: #808080;">&#91;</span>ItemName<span style="color: #808080;">&#93;</span> <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">NVARCHAR</span><span style="color: #808080;">&#93;</span><span style="color: #808080;">&#40;</span><span style="color: #000;">200</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">NOT</span> <span style="color: #808080;">NULL</span>,
	<span style="color: #808080;">&#91;</span>SPSiteID<span style="color: #808080;">&#93;</span> <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">UNIQUEIDENTIFIER</span><span style="color: #808080;">&#93;</span> <span style="color: #808080;">NOT</span> <span style="color: #808080;">NULL</span>,
	<span style="color: #808080;">&#91;</span>SPWebID<span style="color: #808080;">&#93;</span> <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">UNIQUEIDENTIFIER</span><span style="color: #808080;">&#93;</span> <span style="color: #808080;">NOT</span> <span style="color: #808080;">NULL</span>,
	<span style="color: #808080;">&#91;</span>UserIdentity<span style="color: #808080;">&#93;</span> <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">NVARCHAR</span><span style="color: #808080;">&#93;</span><span style="color: #808080;">&#40;</span><span style="color: #000;">450</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">NULL</span>,
	<span style="color: #808080;">&#91;</span>LastModifiedBy<span style="color: #808080;">&#93;</span> <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">NVARCHAR</span><span style="color: #808080;">&#93;</span><span style="color: #808080;">&#40;</span><span style="color: #000;">450</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">NOT</span> <span style="color: #808080;">NULL</span>,
	<span style="color: #808080;">&#91;</span>EmailNotification<span style="color: #808080;">&#93;</span> <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">BIT</span><span style="color: #808080;">&#93;</span> <span style="color: #808080;">NOT</span> <span style="color: #808080;">NULL</span>,
	<span style="color: #808080;">&#91;</span>EmailList<span style="color: #808080;">&#93;</span> <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">NVARCHAR</span><span style="color: #808080;">&#93;</span><span style="color: #808080;">&#40;</span><span style="color: #000;">450</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">NULL</span>,
	<span style="color: #808080;">&#91;</span>SecurityConfiguration<span style="color: #808080;">&#93;</span> <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">INT</span><span style="color: #808080;">&#93;</span> <span style="color: #808080;">NOT</span> <span style="color: #808080;">NULL</span>,
	<span style="color: #808080;">&#91;</span>SSApplicationID<span style="color: #808080;">&#93;</span> <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">NVARCHAR</span><span style="color: #808080;">&#93;</span><span style="color: #808080;">&#40;</span><span style="color: #000;">256</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">NULL</span>,
	<span style="color: #808080;">&#91;</span>ProcessAllDataSources<span style="color: #808080;">&#93;</span> <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">BIT</span><span style="color: #808080;">&#93;</span> <span style="color: #808080;">NOT</span> <span style="color: #808080;">NULL</span>,
	<span style="color: #808080;">&#91;</span>ScheduleDetailID<span style="color: #808080;">&#93;</span> <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">UNIQUEIDENTIFIER</span><span style="color: #808080;">&#93;</span> <span style="color: #808080;">NOT</span> <span style="color: #808080;">NULL</span>,
	<span style="color: #808080;">&#91;</span>ScheduleStartDate<span style="color: #808080;">&#93;</span> <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">DATETIME</span><span style="color: #808080;">&#93;</span> <span style="color: #808080;">NOT</span> <span style="color: #808080;">NULL</span>,
	<span style="color: #808080;">&#91;</span>FrequencyKey<span style="color: #808080;">&#93;</span> <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">NCHAR</span><span style="color: #808080;">&#93;</span><span style="color: #808080;">&#40;</span><span style="color: #000;">1</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">NOT</span> <span style="color: #808080;">NULL</span>,
	<span style="color: #808080;">&#91;</span>RepeatFrequency<span style="color: #808080;">&#93;</span> <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">INT</span><span style="color: #808080;">&#93;</span> <span style="color: #808080;">NOT</span> <span style="color: #808080;">NULL</span>,
	<span style="color: #808080;">&#91;</span>Sunday<span style="color: #808080;">&#93;</span> <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">BIT</span><span style="color: #808080;">&#93;</span> <span style="color: #808080;">NOT</span> <span style="color: #808080;">NULL</span>,
	<span style="color: #808080;">&#91;</span>Monday<span style="color: #808080;">&#93;</span> <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">BIT</span><span style="color: #808080;">&#93;</span> <span style="color: #808080;">NOT</span> <span style="color: #808080;">NULL</span>,
	<span style="color: #808080;">&#91;</span>Tuesday<span style="color: #808080;">&#93;</span> <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">BIT</span><span style="color: #808080;">&#93;</span> <span style="color: #808080;">NOT</span> <span style="color: #808080;">NULL</span>,
	<span style="color: #808080;">&#91;</span>Wednesday<span style="color: #808080;">&#93;</span> <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">BIT</span><span style="color: #808080;">&#93;</span> <span style="color: #808080;">NOT</span> <span style="color: #808080;">NULL</span>,
	<span style="color: #808080;">&#91;</span>Thursday<span style="color: #808080;">&#93;</span> <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">BIT</span><span style="color: #808080;">&#93;</span> <span style="color: #808080;">NOT</span> <span style="color: #808080;">NULL</span>,
	<span style="color: #808080;">&#91;</span>Friday<span style="color: #808080;">&#93;</span> <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">BIT</span><span style="color: #808080;">&#93;</span> <span style="color: #808080;">NOT</span> <span style="color: #808080;">NULL</span>,
	<span style="color: #808080;">&#91;</span>Saturday<span style="color: #808080;">&#93;</span> <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">BIT</span><span style="color: #808080;">&#93;</span> <span style="color: #808080;">NOT</span> <span style="color: #808080;">NULL</span>,
	<span style="color: #808080;">&#91;</span>WeekDay<span style="color: #808080;">&#93;</span> <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">BIT</span><span style="color: #808080;">&#93;</span> <span style="color: #808080;">NOT</span> <span style="color: #808080;">NULL</span>,
	<span style="color: #808080;">&#91;</span>WeekendDay<span style="color: #808080;">&#93;</span> <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">BIT</span><span style="color: #808080;">&#93;</span> <span style="color: #808080;">NOT</span> <span style="color: #808080;">NULL</span>,
	<span style="color: #808080;">&#91;</span><span style="color: #0000FF;">DAY</span><span style="color: #808080;">&#93;</span> <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">BIT</span><span style="color: #808080;">&#93;</span> <span style="color: #808080;">NOT</span> <span style="color: #808080;">NULL</span>,
	<span style="color: #808080;">&#91;</span>MonthlyPeriod<span style="color: #808080;">&#93;</span> <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">TINYINT</span><span style="color: #808080;">&#93;</span> <span style="color: #808080;">NOT</span> <span style="color: #808080;">NULL</span>,
	<span style="color: #808080;">&#91;</span>MonthlySpecificDay<span style="color: #808080;">&#93;</span> <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">INT</span><span style="color: #808080;">&#93;</span> <span style="color: #808080;">NOT</span> <span style="color: #808080;">NULL</span>,
	<span style="color: #808080;">&#91;</span>ProcessAfterBusinessHours<span style="color: #808080;">&#93;</span> <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">BIT</span><span style="color: #808080;">&#93;</span> <span style="color: #808080;">NOT</span> <span style="color: #808080;">NULL</span>,
	<span style="color: #808080;">&#91;</span>SpecificTime<span style="color: #808080;">&#93;</span> <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">DATETIME</span><span style="color: #808080;">&#93;</span> <span style="color: #808080;">NULL</span>
<span style="color: #808080;">&#41;</span>
&nbsp;
<span style="color: #0000FF;">INSERT</span> <span style="color: #0000FF;">INTO</span> @Schedule
<span style="color: #0000FF;">EXEC</span> DataRefresh.<span style="color: #202020;">GetSchedule</span> @ItemID
&nbsp;
<span style="color: #0000FF;">SELECT</span> 
	 @ItemName <span style="color: #808080;">=</span> <span style="color: #808080;">&#91;</span>ItemName<span style="color: #808080;">&#93;</span>
	,@SPSiteID <span style="color: #808080;">=</span> <span style="color: #808080;">&#91;</span>SPSiteID<span style="color: #808080;">&#93;</span>
	,@SPWebID <span style="color: #808080;">=</span> <span style="color: #808080;">&#91;</span>SPWebID<span style="color: #808080;">&#93;</span>
	,@LastModifiedBy <span style="color: #808080;">=</span> <span style="color: #808080;">&#91;</span>LastModifiedBy<span style="color: #808080;">&#93;</span>
	,@ScheduleLastUpdatedBy <span style="color: #808080;">=</span> N<span style="color: #FF0000;">'DOMAIN<span style="color: #000099; font-weight: bold;">\t</span>laqua'</span>
	,@UserIdentity <span style="color: #808080;">=</span> <span style="color: #808080;">&#91;</span>UserIdentity<span style="color: #808080;">&#93;</span>
	,@EmailNotification <span style="color: #808080;">=</span> <span style="color: #808080;">&#91;</span>EmailNotification<span style="color: #808080;">&#93;</span>
	,@EmailList <span style="color: #808080;">=</span> <span style="color: #808080;">&#91;</span>EmailList<span style="color: #808080;">&#93;</span>
	,@SecurityConfiguration <span style="color: #808080;">=</span> <span style="color: #808080;">&#91;</span>SecurityConfiguration<span style="color: #808080;">&#93;</span>
	,@SSApplicationID <span style="color: #808080;">=</span> <span style="color: #808080;">&#91;</span>SSApplicationID<span style="color: #808080;">&#93;</span>
	,@ProcessAllDataSources <span style="color: #808080;">=</span> <span style="color: #808080;">&#91;</span>ProcessAllDataSources<span style="color: #808080;">&#93;</span>
	,@RV <span style="color: #808080;">=</span> <span style="color: #808080;">&#91;</span>RV<span style="color: #808080;">&#93;</span>
		<span style="color: #008080;">-- Schedule info</span>
	,@Enabled <span style="color: #808080;">=</span> <span style="color: #808080;">&#91;</span>Enabled<span style="color: #808080;">&#93;</span>
		<span style="color: #008080;">-- Schedule details</span>
	,@NextProcessDate <span style="color: #808080;">=</span> <span style="color: #0000FF;">CAST</span><span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">GETDATE</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> <span style="color: #0000FF;">DATE</span><span style="color: #808080;">&#41;</span>
	,@ScheduleStartDate <span style="color: #808080;">=</span> <span style="color: #808080;">&#91;</span>ScheduleStartDate<span style="color: #808080;">&#93;</span>
	,@FrequencyKey <span style="color: #808080;">=</span> <span style="color: #808080;">&#91;</span>FrequencyKey<span style="color: #808080;">&#93;</span>
	,@RepeatFrequency <span style="color: #808080;">=</span> <span style="color: #808080;">&#91;</span>RepeatFrequency<span style="color: #808080;">&#93;</span>
	,@Sunday <span style="color: #808080;">=</span> <span style="color: #808080;">&#91;</span>Sunday<span style="color: #808080;">&#93;</span>
	,@Monday <span style="color: #808080;">=</span> <span style="color: #808080;">&#91;</span>Monday<span style="color: #808080;">&#93;</span>
	,@Tuesday <span style="color: #808080;">=</span> <span style="color: #808080;">&#91;</span>Tuesday<span style="color: #808080;">&#93;</span>
	,@Wednesday <span style="color: #808080;">=</span> <span style="color: #808080;">&#91;</span>Wednesday<span style="color: #808080;">&#93;</span>
	,@Thursday <span style="color: #808080;">=</span> <span style="color: #808080;">&#91;</span>Thursday<span style="color: #808080;">&#93;</span>
	,@Friday <span style="color: #808080;">=</span> <span style="color: #808080;">&#91;</span>Friday<span style="color: #808080;">&#93;</span>
	,@Saturday <span style="color: #808080;">=</span> <span style="color: #808080;">&#91;</span>Saturday<span style="color: #808080;">&#93;</span>
	,@Weekday <span style="color: #808080;">=</span> <span style="color: #808080;">&#91;</span>Weekday<span style="color: #808080;">&#93;</span>
	,@WeekendDay <span style="color: #808080;">=</span> <span style="color: #808080;">&#91;</span>WeekendDay<span style="color: #808080;">&#93;</span>
	,@<span style="color: #0000FF;">DAY</span> <span style="color: #808080;">=</span> <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">DAY</span><span style="color: #808080;">&#93;</span>
	,@MonthlyPeriod <span style="color: #808080;">=</span> <span style="color: #808080;">&#91;</span>MonthlyPeriod<span style="color: #808080;">&#93;</span>
	,@MonthlySpecificDay <span style="color: #808080;">=</span> <span style="color: #808080;">&#91;</span>MonthlySpecificDay<span style="color: #808080;">&#93;</span>
	,@ProcessAfterBusinessHours <span style="color: #808080;">=</span> <span style="color: #808080;">&#91;</span>ProcessAfterBusinessHours<span style="color: #808080;">&#93;</span>
	,@SpecificTime <span style="color: #808080;">=</span> <span style="color: #808080;">&#91;</span>SpecificTime<span style="color: #808080;">&#93;</span>
	,@RunNow <span style="color: #808080;">=</span> <span style="color: #000;">1</span>
<span style="color: #0000FF;">FROM</span> @Schedule
&nbsp;
<span style="color: #0000FF;">EXEC</span> DataRefresh.<span style="color: #202020;">PersistSchedule</span>
	 @ItemID <span style="color: #808080;">=</span> @ItemID
	,@ItemName <span style="color: #808080;">=</span> @ItemName
	,@SPSiteID <span style="color: #808080;">=</span> @SPSiteID
	,@SPWebID <span style="color: #808080;">=</span> @SPWebID
	,@LastModifiedBy <span style="color: #808080;">=</span> @LastModifiedBy
	,@ScheduleLastUpdatedBy <span style="color: #808080;">=</span> @ScheduleLastUpdatedBy
	,@UserIdentity <span style="color: #808080;">=</span> @UserIdentity
	,@EmailNotification <span style="color: #808080;">=</span> @EmailNotification
	,@EmailList <span style="color: #808080;">=</span> @EmailList
	,@SecurityConfiguration <span style="color: #808080;">=</span> @SecurityConfiguration
	,@SSApplicationID <span style="color: #808080;">=</span> @SSApplicationID
	,@ProcessAllDataSources <span style="color: #808080;">=</span> @ProcessAllDataSources
	,@RV <span style="color: #808080;">=</span> @RV
		<span style="color: #008080;">-- Schedule info</span>
	,@Enabled <span style="color: #808080;">=</span> @Enabled
		<span style="color: #008080;">-- Schedule details</span>
	,@NextProcessDate <span style="color: #808080;">=</span> @NextProcessDate
	,@ScheduleStartDate <span style="color: #808080;">=</span> @ScheduleStartDate
	,@FrequencyKey <span style="color: #808080;">=</span> @FrequencyKey
	,@RepeatFrequency <span style="color: #808080;">=</span> @RepeatFrequency
	,@Sunday <span style="color: #808080;">=</span> @Sunday
	,@Monday <span style="color: #808080;">=</span> @Monday
	,@Tuesday <span style="color: #808080;">=</span> @Tuesday
	,@Wednesday <span style="color: #808080;">=</span> @Wednesday
	,@Thursday <span style="color: #808080;">=</span> @Thursday
	,@Friday <span style="color: #808080;">=</span> @Friday
	,@Saturday <span style="color: #808080;">=</span> @Saturday
	,@Weekday <span style="color: #808080;">=</span> @Weekday
	,@WeekendDay <span style="color: #808080;">=</span> @WeekendDay
	,@<span style="color: #0000FF;">DAY</span> <span style="color: #808080;">=</span> @<span style="color: #0000FF;">DAY</span>
	,@MonthlyPeriod <span style="color: #808080;">=</span> @MonthlyPeriod
	,@MonthlySpecificDay <span style="color: #808080;">=</span> @MonthlySpecificDay
	,@ProcessAfterBusinessHours <span style="color: #808080;">=</span> @ProcessAfterBusinessHours
	,@SpecificTime <span style="color: #808080;">=</span> @SpecificTime
	,@RunNow <span style="color: #808080;">=</span> @RunNow</pre></div></div>

<p>The only parameters that don't come from GetSchedule are NextRunDate which we set to midnight of today and RunNow which is, of course, 1 and ScheduleLastModifiedBy (I just tossed my username in there, I assume you could use the system identity as well).</p>
<p>And then we set it up to refresh every 10 minutes via Agent job to test:</p>
<p><a href="http://timlaqua.com/wp-content/uploads/2012/03/Schedule-History.jpg"><img src="http://timlaqua.com/wp-content/uploads/2012/03/Schedule-History.jpg" alt="" title="Schedule History" width="535" height="365" class="alignnone size-full wp-image-979" /></a></p>
<p>Happy refreshing!  Let me know if you have any questions.</p>
]]></content:encoded>
			<wfw:commentRss>http://timlaqua.com/2012/03/automating-powerpivot-data-refresh-in-sharepoint-2010/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Slowly Changing Dimensions with MD5 Hashes in SSIS</title>
		<link>http://timlaqua.com/2012/02/slowly-changing-dimensions-with-md5-hashes-in-ssis/</link>
		<comments>http://timlaqua.com/2012/02/slowly-changing-dimensions-with-md5-hashes-in-ssis/#comments</comments>
		<pubDate>Mon, 27 Feb 2012 14:15:27 +0000</pubDate>
		<dc:creator>Tim</dc:creator>
				<category><![CDATA[How-to Guides]]></category>
		<category><![CDATA[Scripts & Code]]></category>
		<category><![CDATA[Thoughts]]></category>
		<category><![CDATA[bi]]></category>
		<category><![CDATA[business intelligence]]></category>
		<category><![CDATA[data warehousing]]></category>

		<guid isPermaLink="false">http://timlaqua.com/?p=911</guid>
		<description><![CDATA[We recently moved away from the 3rd party Checksum component (and all 3rd party components) in SSIS and I wanted to share the pattern we settled on for maintaining our Type 1 Slowly Changing Dimensions (SCDs). There are two things we wanted to address with our new pattern. First, our previous implementation wasn't performing as [...]]]></description>
			<content:encoded><![CDATA[<p>We recently moved away from the 3rd party Checksum component (and all 3rd party components) in SSIS and I wanted to share the pattern we settled on for maintaining our Type 1 Slowly Changing Dimensions (SCDs).  There are two things we wanted to address with our new pattern.  First, our previous implementation wasn't performing as well as we needed it to or generating reliable checksums.  The second was that we wanted to get away from dependencies on custom assemblies in general.  To illustrate the pattern, we're going to build a SCD package off the Adventure Works DW DimCustomer table and skip over the actual source of the business keys and attributes by selecting directly from the completed dimension for now.</p>
<p>First, we assume that our dimension already exists (and we were using some other checksum or MERGE to maintain it).  We have to add a column to store the MD5 hash:</p>

<div class="wp_syntax"><div class="code"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">ALTER</span> <span style="color: #0000FF;">TABLE</span> dbo.<span style="color: #202020;">DimCustomer</span> <span style="color: #0000FF;">ADD</span>
	MD5 <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">34</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">NOT</span> <span style="color: #808080;">NULL</span> <span style="color: #0000FF;">DEFAULT</span> <span style="color: #FF0000;">''</span></pre></div></div>

<p>Second, we need a staging table to store updated/changed rows.  Script out the current dimension as a CREATE, remove all unneeded constraints and indexes, and create a staging table as a heap:</p>

<div class="wp_syntax"><div class="code"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">CREATE</span> <span style="color: #0000FF;">TABLE</span> <span style="color: #808080;">&#91;</span>dbo<span style="color: #808080;">&#93;</span>.<span style="color: #808080;">&#91;</span>Staging_DimCustomer_UpdatedRows<span style="color: #808080;">&#93;</span><span style="color: #808080;">&#40;</span>
	<span style="color: #808080;">&#91;</span>CustomerKey<span style="color: #808080;">&#93;</span> <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">INT</span><span style="color: #808080;">&#93;</span> <span style="color: #808080;">NOT</span> <span style="color: #808080;">NULL</span>,
	<span style="color: #808080;">&#91;</span>GeographyKey<span style="color: #808080;">&#93;</span> <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">INT</span><span style="color: #808080;">&#93;</span> <span style="color: #808080;">NULL</span>,
	<span style="color: #808080;">&#91;</span>CustomerAlternateKey<span style="color: #808080;">&#93;</span> <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">NVARCHAR</span><span style="color: #808080;">&#93;</span><span style="color: #808080;">&#40;</span><span style="color: #000;">15</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">NOT</span> <span style="color: #808080;">NULL</span>,
	<span style="color: #808080;">&#91;</span>Title<span style="color: #808080;">&#93;</span> <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">NVARCHAR</span><span style="color: #808080;">&#93;</span><span style="color: #808080;">&#40;</span><span style="color: #000;">8</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">NULL</span>,
	<span style="color: #808080;">&#91;</span>FirstName<span style="color: #808080;">&#93;</span> <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">NVARCHAR</span><span style="color: #808080;">&#93;</span><span style="color: #808080;">&#40;</span><span style="color: #000;">50</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">NULL</span>,
	<span style="color: #808080;">&#91;</span>MiddleName<span style="color: #808080;">&#93;</span> <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">NVARCHAR</span><span style="color: #808080;">&#93;</span><span style="color: #808080;">&#40;</span><span style="color: #000;">50</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">NULL</span>,
	<span style="color: #808080;">&#91;</span>LastName<span style="color: #808080;">&#93;</span> <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">NVARCHAR</span><span style="color: #808080;">&#93;</span><span style="color: #808080;">&#40;</span><span style="color: #000;">50</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">NULL</span>,
	<span style="color: #808080;">&#91;</span>NameStyle<span style="color: #808080;">&#93;</span> <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">BIT</span><span style="color: #808080;">&#93;</span> <span style="color: #808080;">NULL</span>,
	<span style="color: #808080;">&#91;</span>BirthDate<span style="color: #808080;">&#93;</span> <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">DATETIME</span><span style="color: #808080;">&#93;</span> <span style="color: #808080;">NULL</span>,
	<span style="color: #808080;">&#91;</span>MaritalStatus<span style="color: #808080;">&#93;</span> <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">NCHAR</span><span style="color: #808080;">&#93;</span><span style="color: #808080;">&#40;</span><span style="color: #000;">1</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">NULL</span>,
	<span style="color: #808080;">&#91;</span>Suffix<span style="color: #808080;">&#93;</span> <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">NVARCHAR</span><span style="color: #808080;">&#93;</span><span style="color: #808080;">&#40;</span><span style="color: #000;">10</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">NULL</span>,
	<span style="color: #808080;">&#91;</span>Gender<span style="color: #808080;">&#93;</span> <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">NVARCHAR</span><span style="color: #808080;">&#93;</span><span style="color: #808080;">&#40;</span><span style="color: #000;">1</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">NULL</span>,
	<span style="color: #808080;">&#91;</span>EmailAddress<span style="color: #808080;">&#93;</span> <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">NVARCHAR</span><span style="color: #808080;">&#93;</span><span style="color: #808080;">&#40;</span><span style="color: #000;">50</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">NULL</span>,
	<span style="color: #808080;">&#91;</span>YearlyIncome<span style="color: #808080;">&#93;</span> <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">MONEY</span><span style="color: #808080;">&#93;</span> <span style="color: #808080;">NULL</span>,
	<span style="color: #808080;">&#91;</span>TotalChildren<span style="color: #808080;">&#93;</span> <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">TINYINT</span><span style="color: #808080;">&#93;</span> <span style="color: #808080;">NULL</span>,
	<span style="color: #808080;">&#91;</span>NumberChildrenAtHome<span style="color: #808080;">&#93;</span> <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">TINYINT</span><span style="color: #808080;">&#93;</span> <span style="color: #808080;">NULL</span>,
	<span style="color: #808080;">&#91;</span>EnglishEducation<span style="color: #808080;">&#93;</span> <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">NVARCHAR</span><span style="color: #808080;">&#93;</span><span style="color: #808080;">&#40;</span><span style="color: #000;">40</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">NULL</span>,
	<span style="color: #808080;">&#91;</span>SpanishEducation<span style="color: #808080;">&#93;</span> <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">NVARCHAR</span><span style="color: #808080;">&#93;</span><span style="color: #808080;">&#40;</span><span style="color: #000;">40</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">NULL</span>,
	<span style="color: #808080;">&#91;</span>FrenchEducation<span style="color: #808080;">&#93;</span> <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">NVARCHAR</span><span style="color: #808080;">&#93;</span><span style="color: #808080;">&#40;</span><span style="color: #000;">40</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">NULL</span>,
	<span style="color: #808080;">&#91;</span>EnglishOccupation<span style="color: #808080;">&#93;</span> <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">NVARCHAR</span><span style="color: #808080;">&#93;</span><span style="color: #808080;">&#40;</span><span style="color: #000;">100</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">NULL</span>,
	<span style="color: #808080;">&#91;</span>SpanishOccupation<span style="color: #808080;">&#93;</span> <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">NVARCHAR</span><span style="color: #808080;">&#93;</span><span style="color: #808080;">&#40;</span><span style="color: #000;">100</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">NULL</span>,
	<span style="color: #808080;">&#91;</span>FrenchOccupation<span style="color: #808080;">&#93;</span> <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">NVARCHAR</span><span style="color: #808080;">&#93;</span><span style="color: #808080;">&#40;</span><span style="color: #000;">100</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">NULL</span>,
	<span style="color: #808080;">&#91;</span>HouseOwnerFlag<span style="color: #808080;">&#93;</span> <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">NCHAR</span><span style="color: #808080;">&#93;</span><span style="color: #808080;">&#40;</span><span style="color: #000;">1</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">NULL</span>,
	<span style="color: #808080;">&#91;</span>NumberCarsOwned<span style="color: #808080;">&#93;</span> <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">TINYINT</span><span style="color: #808080;">&#93;</span> <span style="color: #808080;">NULL</span>,
	<span style="color: #808080;">&#91;</span>AddressLine1<span style="color: #808080;">&#93;</span> <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">NVARCHAR</span><span style="color: #808080;">&#93;</span><span style="color: #808080;">&#40;</span><span style="color: #000;">120</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">NULL</span>,
	<span style="color: #808080;">&#91;</span>AddressLine2<span style="color: #808080;">&#93;</span> <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">NVARCHAR</span><span style="color: #808080;">&#93;</span><span style="color: #808080;">&#40;</span><span style="color: #000;">120</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">NULL</span>,
	<span style="color: #808080;">&#91;</span>Phone<span style="color: #808080;">&#93;</span> <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">NVARCHAR</span><span style="color: #808080;">&#93;</span><span style="color: #808080;">&#40;</span><span style="color: #000;">20</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">NULL</span>,
	<span style="color: #808080;">&#91;</span>DateFirstPurchase<span style="color: #808080;">&#93;</span> <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">DATETIME</span><span style="color: #808080;">&#93;</span> <span style="color: #808080;">NULL</span>,
	<span style="color: #808080;">&#91;</span>CommuteDistance<span style="color: #808080;">&#93;</span> <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">NVARCHAR</span><span style="color: #808080;">&#93;</span><span style="color: #808080;">&#40;</span><span style="color: #000;">15</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">NULL</span>,
	<span style="color: #808080;">&#91;</span>MD5<span style="color: #808080;">&#93;</span> <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#93;</span><span style="color: #808080;">&#40;</span><span style="color: #000;">34</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">NOT</span> <span style="color: #808080;">NULL</span><span style="color: #808080;">&#41;</span></pre></div></div>

<p>Now in to SSIS - We will be building:</p>
<ol>
<li>Execute SQL Task to Truncate our Staging table(s)</li>
<li>Data Flow Task to Insert new rows and Stage updated rows</li>
<ol>
<li>OLE DB Source to retrieve our source data</li>
<li>Script Component to Generate Row Numbers</li>
<li>Conditional Split to Evenly Distribute Rows</li>
<li>Script Component to Generate MD5 Hashes</li>
<li>Union All to Squish it all back together</li>
<li>Lookup to get the existing MD5 Hash (if it exists)</li>
<li>Conditional Split to separate Unchanged and Changed rows</li>
<li>RowCount Transformation </li>
<li>OLE DB Destination for Changed rows</li>
<li>OLE DB Destination for New rows</li>
</ol>
<li>Execute SQL Task to Update changed rows</li>
</ol>
<p><em>Completed Control Flow</em><br />
<a href="http://timlaqua.com/wp-content/uploads/2012/02/CompletedControlFlow.png"><img src="http://timlaqua.com/wp-content/uploads/2012/02/CompletedControlFlow.png" alt="" title="CompletedControlFlow" width="268" height="229" class="alignnone size-full wp-image-925" /></a></p>
<p><em>Completed Data Flow</em><br />
<a href="http://timlaqua.com/wp-content/uploads/2012/02/CompletedDataFlow.png"><img src="http://timlaqua.com/wp-content/uploads/2012/02/CompletedDataFlow.png" alt="" title="CompletedDataFlow" width="619" height="636" class="alignnone size-full wp-image-926" /></a></p>
<p><span id="more-911"></span></p>
<h5>1: Execute SQL Task to Truncate our Staging table(s)</h5>

<div class="wp_syntax"><div class="code"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">TRUNCATE</span> <span style="color: #0000FF;">TABLE</span> Staging_DimCustomer_UpdatedRows;</pre></div></div>

<h5>2.1: OLE DB Source to retrieve our source data</h5>
<p><em>Configure the Connection Manager - for the source we'll just select from the dimension minus the surrogate key and MD5 column:</em><br />
<a href="http://timlaqua.com/wp-content/uploads/2012/02/2-1-Query1.png"><img src="http://timlaqua.com/wp-content/uploads/2012/02/2-1-Query1.png" alt="" title="2-1 Query" width="832" height="721" class="alignnone size-full wp-image-954" /></a></p>
<p><em>You most likely won't need to exclude any columns - here we unselect the surrogate key and the MD5 column:</em><br />
<a href="http://timlaqua.com/wp-content/uploads/2012/02/2-1-Columns.png"><img src="http://timlaqua.com/wp-content/uploads/2012/02/2-1-Columns.png" alt="" title="2-1 Columns" width="831" height="719" class="alignnone size-full wp-image-934" /></a></p>
<h5>2.2: Script Component to Generate Row Numbers</h5>
<p><em>Add one output column to Output 0 in your Row Number Script Transformation</em><br />
<a href="http://timlaqua.com/wp-content/uploads/2012/02/2-2-Output-Column.png"><img src="http://timlaqua.com/wp-content/uploads/2012/02/2-2-Output-Column.png" alt="" title="2-2 Output Column" width="838" height="724" class="alignnone size-full wp-image-936" /></a></p>
<p><em>The following code will assign the current row number and increment it for the next row through the buffer:</em></p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">using</span> <span style="color: #008080;">System</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Data</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">Microsoft.SqlServer.Dts.Pipeline.Wrapper</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">Microsoft.SqlServer.Dts.Runtime.Wrapper</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #000000;">&#91;</span>Microsoft.<span style="color: #0000FF;">SqlServer</span>.<span style="color: #0000FF;">Dts</span>.<span style="color: #0000FF;">Pipeline</span>.<span style="color: #0000FF;">SSISScriptComponentEntryPointAttribute</span><span style="color: #000000;">&#93;</span>
<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> ScriptMain <span style="color: #008000;">:</span> UserComponent
<span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">private</span> <span style="color: #FF0000;">int</span> _rowCount <span style="color: #008000;">=</span> <span style="color: #FF0000;">1</span><span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">override</span> <span style="color: #0600FF;">void</span> Input0_ProcessInputRow<span style="color: #000000;">&#40;</span>Input0Buffer Row<span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        Row.<span style="color: #0000FF;">RowNumber</span> <span style="color: #008000;">=</span> _rowCount<span style="color: #008000;">++;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<h5>2.3: Conditional Split to Evenly Distribute Rows</h5>
<p><em>Now in the conditional split, we distribute rows across multiple outputs by using the modulo operator.  Modify to suit the number of threads you need to remove any bottlenecks in the MD5 calculation (you may only need one thread):</em><br />
<a href="http://timlaqua.com/wp-content/uploads/2012/02/2-3-Conditional-Split-Outputs.png"><img src="http://timlaqua.com/wp-content/uploads/2012/02/2-3-Conditional-Split-Outputs.png" alt="" title="2-3 Conditional Split Outputs" width="742" height="721" class="alignnone size-full wp-image-937" /></a></p>
<h5>2.4: Script Component to Generate MD5 Hashes</h5>
<p><em>Add one output column to Output 0 in your Generate MD5 Script Transformation</em><br />
<a href="http://timlaqua.com/wp-content/uploads/2012/02/2-4-Output-Columns.png"><img src="http://timlaqua.com/wp-content/uploads/2012/02/2-4-Output-Columns.png" alt="" title="2-4 Output Columns" width="832" height="721" class="alignnone size-full wp-image-938" /></a></p>
<p>Select all input columns or just select ones that are going to be in the hash.  Since we explicitly specify columns in the script, the hash will only be generated with exactly what columns you tell it to use.</p>
<p><em>The following script will generate a string representation of a MD5 hash.  We do this so that we can do direct comparisons with HASHBYTES converted output (and virtually everything likes dealing with strings better than binary).  We do end up doubling the storage cost of the MD5 hash by storing it as a string, but we were fine with that:</em></p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">using</span> <span style="color: #008080;">System</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Data</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">Microsoft.SqlServer.Dts.Pipeline.Wrapper</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">Microsoft.SqlServer.Dts.Runtime.Wrapper</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #000000;">&#91;</span>Microsoft.<span style="color: #0000FF;">SqlServer</span>.<span style="color: #0000FF;">Dts</span>.<span style="color: #0000FF;">Pipeline</span>.<span style="color: #0000FF;">SSISScriptComponentEntryPointAttribute</span><span style="color: #000000;">&#93;</span>
<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> ScriptMain <span style="color: #008000;">:</span> UserComponent
<span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">private</span> <span style="color: #000000;">System.<span style="color: #0000FF;">Security</span>.<span style="color: #0000FF;">Cryptography</span></span>.<span style="color: #0000FF;">MD5</span> _md5 <span style="color: #008000;">=</span>
                <span style="color: #008000;">new</span> <span style="color: #000000;">System.<span style="color: #0000FF;">Security</span>.<span style="color: #0000FF;">Cryptography</span></span>.<span style="color: #0000FF;">MD5CryptoServiceProvider</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">override</span> <span style="color: #0600FF;">void</span> Input0_ProcessInputRow<span style="color: #000000;">&#40;</span>Input0Buffer Row<span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        <span style="color: #0600FF;">try</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #FF0000;">string</span> hashSource <span style="color: #008000;">=</span>
                <span style="color: #000000;">&#40;</span>Row.<span style="color: #0000FF;">GeographyKey_IsNull</span> <span style="color: #008000;">?</span> <span style="color: #666666;">&quot;&quot;</span> <span style="color: #008000;">:</span> Row.<span style="color: #0000FF;">GeographyKey</span>.<span style="color: #0000FF;">ToString</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
                <span style="color: #008000;">+</span> <span style="color: #000000;">&#40;</span>Row.<span style="color: #0000FF;">Title_IsNull</span> <span style="color: #008000;">?</span> <span style="color: #666666;">&quot;&quot;</span> <span style="color: #008000;">:</span> Row.<span style="color: #0000FF;">Title</span><span style="color: #000000;">&#41;</span>
                <span style="color: #008000;">+</span> <span style="color: #000000;">&#40;</span>Row.<span style="color: #0000FF;">FirstName_IsNull</span> <span style="color: #008000;">?</span> <span style="color: #666666;">&quot;&quot;</span> <span style="color: #008000;">:</span> Row.<span style="color: #0000FF;">FirstName</span><span style="color: #000000;">&#41;</span>
                <span style="color: #008000;">+</span> <span style="color: #000000;">&#40;</span>Row.<span style="color: #0000FF;">MiddleName_IsNull</span> <span style="color: #008000;">?</span> <span style="color: #666666;">&quot;&quot;</span> <span style="color: #008000;">:</span> Row.<span style="color: #0000FF;">MiddleName</span><span style="color: #000000;">&#41;</span>
                <span style="color: #008000;">+</span> <span style="color: #000000;">&#40;</span>Row.<span style="color: #0000FF;">LastName_IsNull</span> <span style="color: #008000;">?</span> <span style="color: #666666;">&quot;&quot;</span> <span style="color: #008000;">:</span> Row.<span style="color: #0000FF;">LastName</span><span style="color: #000000;">&#41;</span>
                <span style="color: #008000;">+</span> <span style="color: #000000;">&#40;</span>Row.<span style="color: #0000FF;">NameStyle_IsNull</span> <span style="color: #008000;">?</span> <span style="color: #666666;">&quot;&quot;</span> <span style="color: #008000;">:</span> Row.<span style="color: #0000FF;">NameStyle</span>.<span style="color: #0000FF;">ToString</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
                <span style="color: #008000;">+</span> <span style="color: #000000;">&#40;</span>Row.<span style="color: #0000FF;">BirthDate_IsNull</span> <span style="color: #008000;">?</span> <span style="color: #666666;">&quot;&quot;</span> <span style="color: #008000;">:</span> Row.<span style="color: #0000FF;">BirthDate</span>.<span style="color: #0000FF;">ToString</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
                <span style="color: #008000;">+</span> <span style="color: #000000;">&#40;</span>Row.<span style="color: #0000FF;">MaritalStatus_IsNull</span> <span style="color: #008000;">?</span> <span style="color: #666666;">&quot;&quot;</span> <span style="color: #008000;">:</span> Row.<span style="color: #0000FF;">MaritalStatus</span><span style="color: #000000;">&#41;</span>
                <span style="color: #008000;">+</span> <span style="color: #000000;">&#40;</span>Row.<span style="color: #0000FF;">Suffix_IsNull</span> <span style="color: #008000;">?</span> <span style="color: #666666;">&quot;&quot;</span> <span style="color: #008000;">:</span> Row.<span style="color: #0000FF;">Suffix</span><span style="color: #000000;">&#41;</span>
                <span style="color: #008000;">+</span> <span style="color: #000000;">&#40;</span>Row.<span style="color: #0000FF;">Gender_IsNull</span> <span style="color: #008000;">?</span> <span style="color: #666666;">&quot;&quot;</span> <span style="color: #008000;">:</span> Row.<span style="color: #0000FF;">Gender</span><span style="color: #000000;">&#41;</span>
                <span style="color: #008000;">+</span> <span style="color: #000000;">&#40;</span>Row.<span style="color: #0000FF;">EmailAddress_IsNull</span> <span style="color: #008000;">?</span> <span style="color: #666666;">&quot;&quot;</span> <span style="color: #008000;">:</span> Row.<span style="color: #0000FF;">EmailAddress</span><span style="color: #000000;">&#41;</span>
                <span style="color: #008000;">+</span> <span style="color: #000000;">&#40;</span>Row.<span style="color: #0000FF;">YearlyIncome_IsNull</span> <span style="color: #008000;">?</span> <span style="color: #666666;">&quot;&quot;</span> <span style="color: #008000;">:</span> Row.<span style="color: #0000FF;">YearlyIncome</span>.<span style="color: #0000FF;">ToString</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
                <span style="color: #008000;">+</span> <span style="color: #000000;">&#40;</span>Row.<span style="color: #0000FF;">TotalChildren_IsNull</span> <span style="color: #008000;">?</span> <span style="color: #666666;">&quot;&quot;</span> <span style="color: #008000;">:</span> Row.<span style="color: #0000FF;">TotalChildren</span>.<span style="color: #0000FF;">ToString</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
                <span style="color: #008000;">+</span> <span style="color: #000000;">&#40;</span>Row.<span style="color: #0000FF;">NumberChildrenAtHome_IsNull</span> <span style="color: #008000;">?</span> <span style="color: #666666;">&quot;&quot;</span> <span style="color: #008000;">:</span> Row.<span style="color: #0000FF;">NumberChildrenAtHome</span>.<span style="color: #0000FF;">ToString</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
                <span style="color: #008000;">+</span> <span style="color: #000000;">&#40;</span>Row.<span style="color: #0000FF;">EnglishEducation_IsNull</span> <span style="color: #008000;">?</span> <span style="color: #666666;">&quot;&quot;</span> <span style="color: #008000;">:</span> Row.<span style="color: #0000FF;">EnglishEducation</span><span style="color: #000000;">&#41;</span>
                <span style="color: #008000;">+</span> <span style="color: #000000;">&#40;</span>Row.<span style="color: #0000FF;">SpanishEducation_IsNull</span> <span style="color: #008000;">?</span> <span style="color: #666666;">&quot;&quot;</span> <span style="color: #008000;">:</span> Row.<span style="color: #0000FF;">SpanishEducation</span><span style="color: #000000;">&#41;</span>
                <span style="color: #008000;">+</span> <span style="color: #000000;">&#40;</span>Row.<span style="color: #0000FF;">FrenchEducation_IsNull</span> <span style="color: #008000;">?</span> <span style="color: #666666;">&quot;&quot;</span> <span style="color: #008000;">:</span> Row.<span style="color: #0000FF;">FrenchEducation</span><span style="color: #000000;">&#41;</span>
                <span style="color: #008000;">+</span> <span style="color: #000000;">&#40;</span>Row.<span style="color: #0000FF;">EnglishOccupation_IsNull</span> <span style="color: #008000;">?</span> <span style="color: #666666;">&quot;&quot;</span> <span style="color: #008000;">:</span> Row.<span style="color: #0000FF;">EnglishOccupation</span><span style="color: #000000;">&#41;</span>
                <span style="color: #008000;">+</span> <span style="color: #000000;">&#40;</span>Row.<span style="color: #0000FF;">SpanishOccupation_IsNull</span> <span style="color: #008000;">?</span> <span style="color: #666666;">&quot;&quot;</span> <span style="color: #008000;">:</span> Row.<span style="color: #0000FF;">SpanishOccupation</span><span style="color: #000000;">&#41;</span>
                <span style="color: #008000;">+</span> <span style="color: #000000;">&#40;</span>Row.<span style="color: #0000FF;">FrenchOccupation_IsNull</span> <span style="color: #008000;">?</span> <span style="color: #666666;">&quot;&quot;</span> <span style="color: #008000;">:</span> Row.<span style="color: #0000FF;">FrenchOccupation</span><span style="color: #000000;">&#41;</span>
                <span style="color: #008000;">+</span> <span style="color: #000000;">&#40;</span>Row.<span style="color: #0000FF;">HouseOwnerFlag_IsNull</span> <span style="color: #008000;">?</span> <span style="color: #666666;">&quot;&quot;</span> <span style="color: #008000;">:</span> Row.<span style="color: #0000FF;">HouseOwnerFlag</span><span style="color: #000000;">&#41;</span>
                <span style="color: #008000;">+</span> <span style="color: #000000;">&#40;</span>Row.<span style="color: #0000FF;">NumberCarsOwned_IsNull</span> <span style="color: #008000;">?</span> <span style="color: #666666;">&quot;&quot;</span> <span style="color: #008000;">:</span> Row.<span style="color: #0000FF;">NumberCarsOwned</span>.<span style="color: #0000FF;">ToString</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
                <span style="color: #008000;">+</span> <span style="color: #000000;">&#40;</span>Row.<span style="color: #0000FF;">AddressLine1_IsNull</span> <span style="color: #008000;">?</span> <span style="color: #666666;">&quot;&quot;</span> <span style="color: #008000;">:</span> Row.<span style="color: #0000FF;">AddressLine1</span><span style="color: #000000;">&#41;</span>
                <span style="color: #008000;">+</span> <span style="color: #000000;">&#40;</span>Row.<span style="color: #0000FF;">AddressLine2_IsNull</span> <span style="color: #008000;">?</span> <span style="color: #666666;">&quot;&quot;</span> <span style="color: #008000;">:</span> Row.<span style="color: #0000FF;">AddressLine2</span><span style="color: #000000;">&#41;</span>
                <span style="color: #008000;">+</span> <span style="color: #000000;">&#40;</span>Row.<span style="color: #0000FF;">Phone_IsNull</span> <span style="color: #008000;">?</span> <span style="color: #666666;">&quot;&quot;</span> <span style="color: #008000;">:</span> Row.<span style="color: #0000FF;">Phone</span><span style="color: #000000;">&#41;</span>
                <span style="color: #008000;">+</span> <span style="color: #000000;">&#40;</span>Row.<span style="color: #0000FF;">DateFirstPurchase_IsNull</span> <span style="color: #008000;">?</span> <span style="color: #666666;">&quot;&quot;</span> <span style="color: #008000;">:</span> Row.<span style="color: #0000FF;">DateFirstPurchase</span>.<span style="color: #0000FF;">ToString</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
                <span style="color: #008000;">+</span> <span style="color: #000000;">&#40;</span>Row.<span style="color: #0000FF;">CommuteDistance_IsNull</span> <span style="color: #008000;">?</span> <span style="color: #666666;">&quot;&quot;</span> <span style="color: #008000;">:</span> Row.<span style="color: #0000FF;">CommuteDistance</span><span style="color: #000000;">&#41;</span>
                <span style="color: #008000;">;</span>
&nbsp;
            <span style="color: #FF0000;">byte</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> hashBytes <span style="color: #008000;">=</span> _md5.<span style="color: #0000FF;">ComputeHash</span><span style="color: #000000;">&#40;</span>
                <span style="color: #000000;">System.<span style="color: #0000FF;">Text</span></span>.<span style="color: #0000FF;">UnicodeEncoding</span>.<span style="color: #0000FF;">Unicode</span>.<span style="color: #0000FF;">GetBytes</span><span style="color: #000000;">&#40;</span>hashSource<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
            <span style="color: #000000;">System.<span style="color: #0000FF;">Text</span></span>.<span style="color: #0000FF;">StringBuilder</span> sb <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> <span style="color: #000000;">System.<span style="color: #0000FF;">Text</span></span>.<span style="color: #0000FF;">StringBuilder</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
            <span style="color: #0600FF;">for</span> <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span> i <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span> i <span style="color: #008000;">&lt;</span> hashBytes.<span style="color: #0000FF;">Length</span><span style="color: #008000;">;</span> i<span style="color: #008000;">++</span><span style="color: #000000;">&#41;</span>
            <span style="color: #000000;">&#123;</span>
                sb.<span style="color: #0000FF;">Append</span><span style="color: #000000;">&#40;</span>hashBytes<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span>.<span style="color: #0000FF;">ToString</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;X2&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #000000;">&#125;</span>
&nbsp;
            Row.<span style="color: #0000FF;">MD5</span> <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;0x&quot;</span> <span style="color: #008000;">+</span> sb.<span style="color: #0000FF;">ToString</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
        <span style="color: #0600FF;">catch</span> <span style="color: #000000;">&#40;</span>Exception e<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            Row.<span style="color: #0000FF;">MD5</span> <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;Error&quot;</span><span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>To quickly generate the string concatenation statement:</p>
<ol>
<li>Script DimCustomer As SELECT to Clipboard and paste in to Notepad++</li>
<li>Search > Replace... (Ctrl+H)</li>
<li>Select Regular expression for Search Mode</li>
<li><strong>Find:</strong> ^.*?\[(.*?)\].*$</li>
<li><strong>Replace:</strong> + (Row.\1_IsNull ? "" : Row.\1)</li>
<li>Replace All, remove the leading +, and remove the garbage at the bottom, remove surrogate and business keys</li>
<li>Paste results in to the script</li>
<li>Add .ToString() to non-string columns and fix any column names (issues will be underlined in red)</li>
</ol>
<p>Copy your completed script component once for each thread you'll be running.  When you copy a script component, you need to actually open the script after copying it and click Edit Script before it will validate.</p>
<h5>2.5: Union All to Squish it all back together</h5>
<h5>2.6: Lookup to get the existing MD5 Hash (if it exists)</h5>
<p><em>Configure the Lookup component to redirect rows with no matches to the No Match output:</em><br />
<a href="http://timlaqua.com/wp-content/uploads/2012/02/2-6-Redirect-Rows-to-No-Match.png"><img src="http://timlaqua.com/wp-content/uploads/2012/02/2-6-Redirect-Rows-to-No-Match.png" alt="" title="2-6 Redirect Rows to No Match" width="832" height="721" class="alignnone size-full wp-image-941" /></a></p>
<p><em>For your connection, select the surrogate key, business key(s), and MD5 hash from your existing dimension table:</em><br />
<a href="http://timlaqua.com/wp-content/uploads/2012/02/2-6-Connection-Settings.png"><img src="http://timlaqua.com/wp-content/uploads/2012/02/2-6-Connection-Settings.png" alt="" title="2-6 Connection Settings" width="832" height="721" class="alignnone size-full wp-image-940" /></a></p>
<p><em>Lookup based on the business key(s) and bring back the existing MD5 and surrogate key</em><br />
<a href="http://timlaqua.com/wp-content/uploads/2012/02/2-6-Column-Settings.png"><img src="http://timlaqua.com/wp-content/uploads/2012/02/2-6-Column-Settings.png" alt="" title="2-6 Column Settings" width="832" height="721" class="alignnone size-full wp-image-939" /></a></p>
<h5>2.7: Conditional Split to separate Unchanged and Changed rows</h5>
<p><em>Name the output for rows where the ExistingMD5 is the same as the new MD5 "Ignore" and name the Default output "Update"</em><br />
<a href="http://timlaqua.com/wp-content/uploads/2012/02/2-7-Conditional-Outputs.png"><img src="http://timlaqua.com/wp-content/uploads/2012/02/2-7-Conditional-Outputs.png" alt="" title="2-7 Conditional Outputs" width="742" height="721" class="alignnone size-full wp-image-942" /></a></p>
<h5>2.8: RowCount Transformation for Ignored Rows</h5>
<p><em>For your Ignored Rows Row Count transformation, you'll have to create a variable to store the rowcount in (we don't use it, but that's just how the transformation works):</em><br />
<a href="http://timlaqua.com/wp-content/uploads/2012/02/2-8-Ignored-Rows-Settings.png"><img src="http://timlaqua.com/wp-content/uploads/2012/02/2-8-Ignored-Rows-Settings.png" alt="" title="2-8 Ignored Rows Settings" width="799" height="721" class="alignnone size-full wp-image-943" /></a></p>
<h5>2.9: OLE DB Destination for Changed rows</h5>
<p><em>We'll insert rows that need to be updated in to the previously created Staging_DimCustomer_UpdatedRows table:</em><br />
<a href="http://timlaqua.com/wp-content/uploads/2012/02/2-9-Connection-Manager.png"><img src="http://timlaqua.com/wp-content/uploads/2012/02/2-9-Connection-Manager.png" alt="" title="2-9 Connection Manager" width="832" height="721" class="alignnone size-full wp-image-944" /></a></p>
<h5>2.10: OLE DB Destination for New rows</h5>
<p><em>Finally, insert new rows directly in to the dimension:</em><br />
<a href="http://timlaqua.com/wp-content/uploads/2012/02/2-10-Connection-Manager.png"><img src="http://timlaqua.com/wp-content/uploads/2012/02/2-10-Connection-Manager.png" alt="" title="2-10 Connection Manager" width="832" height="721" class="alignnone size-full wp-image-945" /></a></p>
<h5>3: Execute SQL Task to Update changed rows</h5>
<p><em>The final step is to take those rows we staged for updating and actually perform the update:</em></p>

<div class="wp_syntax"><div class="code"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">UPDATE</span> a <span style="color: #0000FF;">SET</span>
 <span style="color: #808080;">&#91;</span>GeographyKey<span style="color: #808080;">&#93;</span> <span style="color: #808080;">=</span> b.<span style="color: #808080;">&#91;</span>GeographyKey<span style="color: #808080;">&#93;</span>
,<span style="color: #808080;">&#91;</span>CustomerAlternateKey<span style="color: #808080;">&#93;</span> <span style="color: #808080;">=</span> b.<span style="color: #808080;">&#91;</span>CustomerAlternateKey<span style="color: #808080;">&#93;</span>
,<span style="color: #808080;">&#91;</span>Title<span style="color: #808080;">&#93;</span> <span style="color: #808080;">=</span> b.<span style="color: #808080;">&#91;</span>Title<span style="color: #808080;">&#93;</span>
,<span style="color: #808080;">&#91;</span>FirstName<span style="color: #808080;">&#93;</span> <span style="color: #808080;">=</span> b.<span style="color: #808080;">&#91;</span>FirstName<span style="color: #808080;">&#93;</span>
,<span style="color: #808080;">&#91;</span>MiddleName<span style="color: #808080;">&#93;</span> <span style="color: #808080;">=</span> b.<span style="color: #808080;">&#91;</span>MiddleName<span style="color: #808080;">&#93;</span>
,<span style="color: #808080;">&#91;</span>LastName<span style="color: #808080;">&#93;</span> <span style="color: #808080;">=</span> b.<span style="color: #808080;">&#91;</span>LastName<span style="color: #808080;">&#93;</span>
,<span style="color: #808080;">&#91;</span>NameStyle<span style="color: #808080;">&#93;</span> <span style="color: #808080;">=</span> b.<span style="color: #808080;">&#91;</span>NameStyle<span style="color: #808080;">&#93;</span>
,<span style="color: #808080;">&#91;</span>BirthDate<span style="color: #808080;">&#93;</span> <span style="color: #808080;">=</span> b.<span style="color: #808080;">&#91;</span>BirthDate<span style="color: #808080;">&#93;</span>
,<span style="color: #808080;">&#91;</span>MaritalStatus<span style="color: #808080;">&#93;</span> <span style="color: #808080;">=</span> b.<span style="color: #808080;">&#91;</span>MaritalStatus<span style="color: #808080;">&#93;</span>
,<span style="color: #808080;">&#91;</span>Suffix<span style="color: #808080;">&#93;</span> <span style="color: #808080;">=</span> b.<span style="color: #808080;">&#91;</span>Suffix<span style="color: #808080;">&#93;</span>
,<span style="color: #808080;">&#91;</span>Gender<span style="color: #808080;">&#93;</span> <span style="color: #808080;">=</span> b.<span style="color: #808080;">&#91;</span>Gender<span style="color: #808080;">&#93;</span>
,<span style="color: #808080;">&#91;</span>EmailAddress<span style="color: #808080;">&#93;</span> <span style="color: #808080;">=</span> b.<span style="color: #808080;">&#91;</span>EmailAddress<span style="color: #808080;">&#93;</span>
,<span style="color: #808080;">&#91;</span>YearlyIncome<span style="color: #808080;">&#93;</span> <span style="color: #808080;">=</span> b.<span style="color: #808080;">&#91;</span>YearlyIncome<span style="color: #808080;">&#93;</span>
,<span style="color: #808080;">&#91;</span>TotalChildren<span style="color: #808080;">&#93;</span> <span style="color: #808080;">=</span> b.<span style="color: #808080;">&#91;</span>TotalChildren<span style="color: #808080;">&#93;</span>
,<span style="color: #808080;">&#91;</span>NumberChildrenAtHome<span style="color: #808080;">&#93;</span> <span style="color: #808080;">=</span> b.<span style="color: #808080;">&#91;</span>NumberChildrenAtHome<span style="color: #808080;">&#93;</span>
,<span style="color: #808080;">&#91;</span>EnglishEducation<span style="color: #808080;">&#93;</span> <span style="color: #808080;">=</span> b.<span style="color: #808080;">&#91;</span>EnglishEducation<span style="color: #808080;">&#93;</span>
,<span style="color: #808080;">&#91;</span>SpanishEducation<span style="color: #808080;">&#93;</span> <span style="color: #808080;">=</span> b.<span style="color: #808080;">&#91;</span>SpanishEducation<span style="color: #808080;">&#93;</span>
,<span style="color: #808080;">&#91;</span>FrenchEducation<span style="color: #808080;">&#93;</span> <span style="color: #808080;">=</span> b.<span style="color: #808080;">&#91;</span>FrenchEducation<span style="color: #808080;">&#93;</span>
,<span style="color: #808080;">&#91;</span>EnglishOccupation<span style="color: #808080;">&#93;</span> <span style="color: #808080;">=</span> b.<span style="color: #808080;">&#91;</span>EnglishOccupation<span style="color: #808080;">&#93;</span>
,<span style="color: #808080;">&#91;</span>SpanishOccupation<span style="color: #808080;">&#93;</span> <span style="color: #808080;">=</span> b.<span style="color: #808080;">&#91;</span>SpanishOccupation<span style="color: #808080;">&#93;</span>
,<span style="color: #808080;">&#91;</span>FrenchOccupation<span style="color: #808080;">&#93;</span> <span style="color: #808080;">=</span> b.<span style="color: #808080;">&#91;</span>FrenchOccupation<span style="color: #808080;">&#93;</span>
,<span style="color: #808080;">&#91;</span>HouseOwnerFlag<span style="color: #808080;">&#93;</span> <span style="color: #808080;">=</span> b.<span style="color: #808080;">&#91;</span>HouseOwnerFlag<span style="color: #808080;">&#93;</span>
,<span style="color: #808080;">&#91;</span>NumberCarsOwned<span style="color: #808080;">&#93;</span> <span style="color: #808080;">=</span> b.<span style="color: #808080;">&#91;</span>NumberCarsOwned<span style="color: #808080;">&#93;</span>
,<span style="color: #808080;">&#91;</span>AddressLine1<span style="color: #808080;">&#93;</span> <span style="color: #808080;">=</span> b.<span style="color: #808080;">&#91;</span>AddressLine1<span style="color: #808080;">&#93;</span>
,<span style="color: #808080;">&#91;</span>AddressLine2<span style="color: #808080;">&#93;</span> <span style="color: #808080;">=</span> b.<span style="color: #808080;">&#91;</span>AddressLine2<span style="color: #808080;">&#93;</span>
,<span style="color: #808080;">&#91;</span>Phone<span style="color: #808080;">&#93;</span> <span style="color: #808080;">=</span> b.<span style="color: #808080;">&#91;</span>Phone<span style="color: #808080;">&#93;</span>
,<span style="color: #808080;">&#91;</span>DateFirstPurchase<span style="color: #808080;">&#93;</span> <span style="color: #808080;">=</span> b.<span style="color: #808080;">&#91;</span>DateFirstPurchase<span style="color: #808080;">&#93;</span>
,<span style="color: #808080;">&#91;</span>CommuteDistance<span style="color: #808080;">&#93;</span> <span style="color: #808080;">=</span> b.<span style="color: #808080;">&#91;</span>CommuteDistance<span style="color: #808080;">&#93;</span>
,<span style="color: #808080;">&#91;</span>MD5<span style="color: #808080;">&#93;</span> <span style="color: #808080;">=</span> b.<span style="color: #808080;">&#91;</span>MD5<span style="color: #808080;">&#93;</span> 
<span style="color: #0000FF;">FROM</span> DimCustomer a
	 <span style="color: #0000FF;">INNER</span> <span style="color: #808080;">JOIN</span> Staging_DimCustomer_UpdatedRows b
		<span style="color: #0000FF;">ON</span> a.<span style="color: #202020;">CustomerKey</span> <span style="color: #808080;">=</span> b.<span style="color: #202020;">CustomerKey</span></pre></div></div>

<p>To quickly generate the update statement:</p>
<ol>
<li>Script DimCustomer As SELECT to Clipboard and paste in to Notepad++</li>
<li>Search > Replace... (Ctrl+H)</li>
<li>Select Regular expression for Search Mode</li>
<li><strong>Find:</strong> ^.*?\[(.*?)\].*$</li>
<li><strong>Replace:</strong> ,[\1] = b.[\1]</li>
<li>Replace All, remove surrogate key and garbage at the bottom, remove leading comma</li>
<li>Add UPDATE a SET at the top</li>
<li>Add appropriate FROM clause at the bottom</li>
</ol>
<p>And bob's your uncle.</p>
<h5>In closing...</h5>
<p><strong>Why not write a reusable code block for the MD5 hash that you could cut and paste?  Why hardcode columns?</strong><br />
Well, there's really only two ways to do it dynamically.  One is to loop over each column in the InputBuffer and concatenate them.  Man, that's a bunch of extra logic when we know very well what columns are, null handling is a little sketch, and proper datatype handling requires some special logic.  Looping over the InputBuffer also just plain feels dirty.  The second option is Reflection.  Now we could approach reflection two ways - first we could iterate over the Row definition and concatenate the columns for each InputRow, but this is INCREDIBLY expensive (reflection is crazy slow).  The second Reflection approach would be to construct a DynamicMethod at runtime and call that dynamic method for each row.  While this could certainly be as fast as hardcoding, it's very obscure and difficult for most people to understand.  Do you want code in your packages that's hard to understand?</p>
<p>We chose hardcode the columns because this is the absolute minimum amount of work that needs to be done to generate a reliable MD5 hash and is therefore the fastest possible solution.</p>
<p><strong>Why not do that Reflection and DynamicMethod thing in a custom transformation?</strong><br />
Because one of the main goals here was to rid ourselves of 3rd party dependencies (all custom assembly dependencies really) and we wanted to do the minimum amount of work required to generated a reliable MD5 hash.</p>
<p><strong>Why bulk update at the end rather than update each row via OLE DB Command?</strong><br />
Because the OLE DB Command isn't set based and it can block/be blocked by inserts.  Also, parameter mapping to all those Param_XX parameters and counting question marks is just terrible.  So, so terrible.</p>
<p><strong>How do I know how many threads I need to distribute the MD5 calculation over?</strong><br />
First we'll usually watch the source query in the database engine to see what it's waiting on.  PREEMPTIVE_OS_WAITFORSINGLEOBJECT tends to point at SQL Server waiting to send data to SSIS because it's not accepting rows fast enough.  For a starting point, take your source query and see how long it takes to just insert the entire thing in to a heap - somewhere around there is what we're shooting for.  Faster if you ignore many rows, slower if you update/insert many rows.</p>
<p><strong>How does it perform?</strong><br />
It's ridiculously fast.  One of our dimensions (55 columns, varying datatypes, ~5 million deep, ~4.5 million evaluated for changes 3 times a day) went from 50+ minutes per run to 5-9 minutes per run.  More importantly, we were previously using the CRC32 algorithm in the Checksum transform and that wasn't producing reliable checksums so we would either update unchanged rows or even miss changes to rows - now the hashes are dead on and reproducible via HASHBYTES.</p>
<p>As a side note, we were able to get Checksum to keep up with sql server by spreading it across 6 threads but the lack of reliable output forced us to abandon it.</p>
<p><strong>Is there a quick way to mimic the SSIS MD5 using HASHBYTES?</strong></p>
<ol>
<li>Script DimCustomer As SELECT to Clipboard and paste in to Notepad++</li>
<li>Search > Replace... (Ctrl+H)</li>
<li>Select Regular expression for Search Mode</li>
<li><strong>Find:</strong> ^.*?\[(.*?)\].*$</li>
<li><strong>Replace:</strong> +ISNULL(CAST([\1] AS NVARCHAR(255)),N'')</li>
<li>Replace All, remove surrogate key, business key(s), leading +, and garbage at the bottom</li>
<li>Add <strong>CONVERT(VARCHAR(34), HASHBYTES('md5',</strong> at the top</li>
<li>Add <strong>),1) AS [Hashbytes]</strong> and an appropriate FROM clause at the bottom</li>
</ol>
<p>Now if all your columns are chars, nchars, varchars, nvarchars, ints, and decimals - you're good.  If some of your columns are the more unique datatypes (datetime, money, bit, etc) - then you've got more work to do.  The issue lies in the difference between how sql server converts these datatypes to NVARCHAR and how C# converts them to strings.  Here are a few examples:</p>
<table cellpadding=0 cellspacing="0">
<tr>
<th>Datatype</th>
<th>SQL Conversion</th>
<th>C# Conversion</th>
</tr>
<tr>
<td>DATETIME</td>
<td>1966-04-08 00:00:00.000</td>
<td>4/8/1966 12:00:00 AM</td>
</tr>
<tr>
<td>MONEY</td>
<td>70000.00</td>
<td>70000</td>
</tr>
<tr>
<td>BIT</td>
<td>1</td>
<td>True</td>
</tr>
<tr>
<td>BIT</td>
<td>0</td>
<td>False</td>
</tr>
</table>
<p>The dates are the biggest debacle as they're sensitive to localization settings.  If you actually had to generate the hashes using HASHBYTES on a regular basis, we would recommend modifying the date formatting when concatenating columns for the hashSource variable in the script so they match sql server.  For us, we only generate MD5s using HASHBYTES for troubleshooting and updating the hash using purely dim columns rather than relying on the source as some business keys no longer exist in the source.</p>
<p><em>Here's the finished tsql for the above hash:</em></p>

<div class="wp_syntax"><div class="code"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">SELECT</span> 
 CustomerKey
,CustomerAlternateKey
,MD5
,<span style="color: #0000FF;">CONVERT</span><span style="color: #808080;">&#40;</span><span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">34</span><span style="color: #808080;">&#41;</span>, HASHBYTES<span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'md5'</span>,
IS<span style="color: #808080;">NULL</span><span style="color: #808080;">&#40;</span><span style="color: #0000FF;">CAST</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#91;</span>GeographyKey<span style="color: #808080;">&#93;</span> <span style="color: #0000FF;">AS</span> <span style="color: #0000FF;">NVARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">255</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span>,N<span style="color: #FF0000;">''</span><span style="color: #808080;">&#41;</span>
<span style="color: #808080;">+</span>IS<span style="color: #808080;">NULL</span><span style="color: #808080;">&#40;</span><span style="color: #0000FF;">CAST</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#91;</span>Title<span style="color: #808080;">&#93;</span> <span style="color: #0000FF;">AS</span> <span style="color: #0000FF;">NVARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">255</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span>,N<span style="color: #FF0000;">''</span><span style="color: #808080;">&#41;</span>
<span style="color: #808080;">+</span>IS<span style="color: #808080;">NULL</span><span style="color: #808080;">&#40;</span><span style="color: #0000FF;">CAST</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#91;</span>FirstName<span style="color: #808080;">&#93;</span> <span style="color: #0000FF;">AS</span> <span style="color: #0000FF;">NVARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">255</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span>,N<span style="color: #FF0000;">''</span><span style="color: #808080;">&#41;</span>
<span style="color: #808080;">+</span>IS<span style="color: #808080;">NULL</span><span style="color: #808080;">&#40;</span><span style="color: #0000FF;">CAST</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#91;</span>MiddleName<span style="color: #808080;">&#93;</span> <span style="color: #0000FF;">AS</span> <span style="color: #0000FF;">NVARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">255</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span>,N<span style="color: #FF0000;">''</span><span style="color: #808080;">&#41;</span>
<span style="color: #808080;">+</span>IS<span style="color: #808080;">NULL</span><span style="color: #808080;">&#40;</span><span style="color: #0000FF;">CAST</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#91;</span>LastName<span style="color: #808080;">&#93;</span> <span style="color: #0000FF;">AS</span> <span style="color: #0000FF;">NVARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">255</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span>,N<span style="color: #FF0000;">''</span><span style="color: #808080;">&#41;</span>
<span style="color: #808080;">+</span>IS<span style="color: #808080;">NULL</span><span style="color: #808080;">&#40;</span><span style="color: #0000FF;">CASE</span> <span style="color: #0000FF;">WHEN</span> <span style="color: #808080;">&#91;</span>NameStyle<span style="color: #808080;">&#93;</span> <span style="color: #808080;">=</span> <span style="color: #000;">1</span> <span style="color: #0000FF;">THEN</span> N<span style="color: #FF0000;">'True'</span> <span style="color: #0000FF;">ELSE</span> N<span style="color: #FF0000;">'False'</span> <span style="color: #0000FF;">END</span>,N<span style="color: #FF0000;">''</span><span style="color: #808080;">&#41;</span>
<span style="color: #808080;">+</span>IS<span style="color: #808080;">NULL</span><span style="color: #808080;">&#40;</span>
<span style="color: #0000FF;">CAST</span><span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">DATEPART</span><span style="color: #808080;">&#40;</span><span style="color: #0000FF;">MONTH</span>,<span style="color: #808080;">&#91;</span>BirthDate<span style="color: #808080;">&#93;</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> <span style="color: #0000FF;">NVARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">2</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span>
<span style="color: #808080;">+</span><span style="color: #FF0000;">'/'</span><span style="color: #808080;">+</span><span style="color: #0000FF;">CAST</span><span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">DATEPART</span><span style="color: #808080;">&#40;</span><span style="color: #0000FF;">DAY</span>,<span style="color: #808080;">&#91;</span>BirthDate<span style="color: #808080;">&#93;</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> <span style="color: #0000FF;">NVARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">2</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span>
<span style="color: #808080;">+</span><span style="color: #FF0000;">'/'</span><span style="color: #808080;">+</span><span style="color: #0000FF;">CAST</span><span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">DATEPART</span><span style="color: #808080;">&#40;</span><span style="color: #0000FF;">YEAR</span>,<span style="color: #808080;">&#91;</span>BirthDate<span style="color: #808080;">&#93;</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> <span style="color: #0000FF;">NVARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">4</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span>
<span style="color: #808080;">+</span><span style="color: #FF0000;">' '</span><span style="color: #808080;">+</span><span style="color: #0000FF;">LEFT</span><span style="color: #808080;">&#40;</span><span style="color: #0000FF;">RIGHT</span><span style="color: #808080;">&#40;</span><span style="color: #0000FF;">CONVERT</span><span style="color: #808080;">&#40;</span><span style="color: #0000FF;">NVARCHAR</span>, <span style="color: #808080;">&#91;</span>BirthDate<span style="color: #808080;">&#93;</span>, <span style="color: #000;">109</span><span style="color: #808080;">&#41;</span>, <span style="color: #000;">14</span><span style="color: #808080;">&#41;</span>,<span style="color: #000;">8</span><span style="color: #808080;">&#41;</span>
<span style="color: #808080;">+</span><span style="color: #FF0000;">' '</span><span style="color: #808080;">+</span><span style="color: #0000FF;">RIGHT</span><span style="color: #808080;">&#40;</span><span style="color: #0000FF;">CONVERT</span><span style="color: #808080;">&#40;</span><span style="color: #0000FF;">NVARCHAR</span>, <span style="color: #808080;">&#91;</span>BirthDate<span style="color: #808080;">&#93;</span>, <span style="color: #000;">109</span><span style="color: #808080;">&#41;</span>, <span style="color: #000;">2</span><span style="color: #808080;">&#41;</span>
,N<span style="color: #FF0000;">''</span><span style="color: #808080;">&#41;</span>
<span style="color: #808080;">+</span>IS<span style="color: #808080;">NULL</span><span style="color: #808080;">&#40;</span><span style="color: #0000FF;">CAST</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#91;</span>MaritalStatus<span style="color: #808080;">&#93;</span> <span style="color: #0000FF;">AS</span> <span style="color: #0000FF;">NVARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">255</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span>,N<span style="color: #FF0000;">''</span><span style="color: #808080;">&#41;</span>
<span style="color: #808080;">+</span>IS<span style="color: #808080;">NULL</span><span style="color: #808080;">&#40;</span><span style="color: #0000FF;">CAST</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#91;</span>Suffix<span style="color: #808080;">&#93;</span> <span style="color: #0000FF;">AS</span> <span style="color: #0000FF;">NVARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">255</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span>,N<span style="color: #FF0000;">''</span><span style="color: #808080;">&#41;</span>
<span style="color: #808080;">+</span>IS<span style="color: #808080;">NULL</span><span style="color: #808080;">&#40;</span><span style="color: #0000FF;">CAST</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#91;</span>Gender<span style="color: #808080;">&#93;</span> <span style="color: #0000FF;">AS</span> <span style="color: #0000FF;">NVARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">255</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span>,N<span style="color: #FF0000;">''</span><span style="color: #808080;">&#41;</span>
<span style="color: #808080;">+</span>IS<span style="color: #808080;">NULL</span><span style="color: #808080;">&#40;</span><span style="color: #0000FF;">CAST</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#91;</span>EmailAddress<span style="color: #808080;">&#93;</span> <span style="color: #0000FF;">AS</span> <span style="color: #0000FF;">NVARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">255</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span>,N<span style="color: #FF0000;">''</span><span style="color: #808080;">&#41;</span>
<span style="color: #808080;">+</span>IS<span style="color: #808080;">NULL</span><span style="color: #808080;">&#40;</span><span style="color: #0000FF;">CAST</span><span style="color: #808080;">&#40;</span><span style="color: #0000FF;">CAST</span><span style="color: #808080;">&#40;</span>YearlyIncome <span style="color: #0000FF;">AS</span> <span style="color: #0000FF;">FLOAT</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> <span style="color: #0000FF;">NVARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">255</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span>,N<span style="color: #FF0000;">''</span><span style="color: #808080;">&#41;</span>
<span style="color: #808080;">+</span>IS<span style="color: #808080;">NULL</span><span style="color: #808080;">&#40;</span><span style="color: #0000FF;">CAST</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#91;</span>TotalChildren<span style="color: #808080;">&#93;</span> <span style="color: #0000FF;">AS</span> <span style="color: #0000FF;">NVARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">255</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span>,N<span style="color: #FF0000;">''</span><span style="color: #808080;">&#41;</span>
<span style="color: #808080;">+</span>IS<span style="color: #808080;">NULL</span><span style="color: #808080;">&#40;</span><span style="color: #0000FF;">CAST</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#91;</span>NumberChildrenAtHome<span style="color: #808080;">&#93;</span> <span style="color: #0000FF;">AS</span> <span style="color: #0000FF;">NVARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">255</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span>,N<span style="color: #FF0000;">''</span><span style="color: #808080;">&#41;</span>
<span style="color: #808080;">+</span>IS<span style="color: #808080;">NULL</span><span style="color: #808080;">&#40;</span><span style="color: #0000FF;">CAST</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#91;</span>EnglishEducation<span style="color: #808080;">&#93;</span> <span style="color: #0000FF;">AS</span> <span style="color: #0000FF;">NVARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">255</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span>,N<span style="color: #FF0000;">''</span><span style="color: #808080;">&#41;</span>
<span style="color: #808080;">+</span>IS<span style="color: #808080;">NULL</span><span style="color: #808080;">&#40;</span><span style="color: #0000FF;">CAST</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#91;</span>SpanishEducation<span style="color: #808080;">&#93;</span> <span style="color: #0000FF;">AS</span> <span style="color: #0000FF;">NVARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">255</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span>,N<span style="color: #FF0000;">''</span><span style="color: #808080;">&#41;</span>
<span style="color: #808080;">+</span>IS<span style="color: #808080;">NULL</span><span style="color: #808080;">&#40;</span><span style="color: #0000FF;">CAST</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#91;</span>FrenchEducation<span style="color: #808080;">&#93;</span> <span style="color: #0000FF;">AS</span> <span style="color: #0000FF;">NVARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">255</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span>,N<span style="color: #FF0000;">''</span><span style="color: #808080;">&#41;</span>
<span style="color: #808080;">+</span>IS<span style="color: #808080;">NULL</span><span style="color: #808080;">&#40;</span><span style="color: #0000FF;">CAST</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#91;</span>EnglishOccupation<span style="color: #808080;">&#93;</span> <span style="color: #0000FF;">AS</span> <span style="color: #0000FF;">NVARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">255</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span>,N<span style="color: #FF0000;">''</span><span style="color: #808080;">&#41;</span>
<span style="color: #808080;">+</span>IS<span style="color: #808080;">NULL</span><span style="color: #808080;">&#40;</span><span style="color: #0000FF;">CAST</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#91;</span>SpanishOccupation<span style="color: #808080;">&#93;</span> <span style="color: #0000FF;">AS</span> <span style="color: #0000FF;">NVARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">255</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span>,N<span style="color: #FF0000;">''</span><span style="color: #808080;">&#41;</span>
<span style="color: #808080;">+</span>IS<span style="color: #808080;">NULL</span><span style="color: #808080;">&#40;</span><span style="color: #0000FF;">CAST</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#91;</span>FrenchOccupation<span style="color: #808080;">&#93;</span> <span style="color: #0000FF;">AS</span> <span style="color: #0000FF;">NVARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">255</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span>,N<span style="color: #FF0000;">''</span><span style="color: #808080;">&#41;</span>
<span style="color: #808080;">+</span>IS<span style="color: #808080;">NULL</span><span style="color: #808080;">&#40;</span><span style="color: #0000FF;">CAST</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#91;</span>HouseOwnerFlag<span style="color: #808080;">&#93;</span> <span style="color: #0000FF;">AS</span> <span style="color: #0000FF;">NVARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">255</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span>,N<span style="color: #FF0000;">''</span><span style="color: #808080;">&#41;</span>
<span style="color: #808080;">+</span>IS<span style="color: #808080;">NULL</span><span style="color: #808080;">&#40;</span><span style="color: #0000FF;">CAST</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#91;</span>NumberCarsOwned<span style="color: #808080;">&#93;</span> <span style="color: #0000FF;">AS</span> <span style="color: #0000FF;">NVARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">255</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span>,N<span style="color: #FF0000;">''</span><span style="color: #808080;">&#41;</span>
<span style="color: #808080;">+</span>IS<span style="color: #808080;">NULL</span><span style="color: #808080;">&#40;</span><span style="color: #0000FF;">CAST</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#91;</span>AddressLine1<span style="color: #808080;">&#93;</span> <span style="color: #0000FF;">AS</span> <span style="color: #0000FF;">NVARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">255</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span>,N<span style="color: #FF0000;">''</span><span style="color: #808080;">&#41;</span>
<span style="color: #808080;">+</span>IS<span style="color: #808080;">NULL</span><span style="color: #808080;">&#40;</span><span style="color: #0000FF;">CAST</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#91;</span>AddressLine2<span style="color: #808080;">&#93;</span> <span style="color: #0000FF;">AS</span> <span style="color: #0000FF;">NVARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">255</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span>,N<span style="color: #FF0000;">''</span><span style="color: #808080;">&#41;</span>
<span style="color: #808080;">+</span>IS<span style="color: #808080;">NULL</span><span style="color: #808080;">&#40;</span><span style="color: #0000FF;">CAST</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#91;</span>Phone<span style="color: #808080;">&#93;</span> <span style="color: #0000FF;">AS</span> <span style="color: #0000FF;">NVARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">255</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span>,N<span style="color: #FF0000;">''</span><span style="color: #808080;">&#41;</span>
<span style="color: #808080;">+</span>IS<span style="color: #808080;">NULL</span><span style="color: #808080;">&#40;</span>
<span style="color: #0000FF;">CAST</span><span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">DATEPART</span><span style="color: #808080;">&#40;</span><span style="color: #0000FF;">MONTH</span>,DateFirstPurchase<span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> <span style="color: #0000FF;">NVARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">2</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span>
<span style="color: #808080;">+</span><span style="color: #FF0000;">'/'</span><span style="color: #808080;">+</span><span style="color: #0000FF;">CAST</span><span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">DATEPART</span><span style="color: #808080;">&#40;</span><span style="color: #0000FF;">DAY</span>,DateFirstPurchase<span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> <span style="color: #0000FF;">NVARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">2</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span>
<span style="color: #808080;">+</span><span style="color: #FF0000;">'/'</span><span style="color: #808080;">+</span><span style="color: #0000FF;">CAST</span><span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">DATEPART</span><span style="color: #808080;">&#40;</span><span style="color: #0000FF;">YEAR</span>,DateFirstPurchase<span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> <span style="color: #0000FF;">NVARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">4</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span>
<span style="color: #808080;">+</span><span style="color: #FF0000;">' '</span><span style="color: #808080;">+</span><span style="color: #0000FF;">LEFT</span><span style="color: #808080;">&#40;</span><span style="color: #0000FF;">RIGHT</span><span style="color: #808080;">&#40;</span><span style="color: #0000FF;">CONVERT</span><span style="color: #808080;">&#40;</span><span style="color: #0000FF;">NVARCHAR</span>, DateFirstPurchase, <span style="color: #000;">109</span><span style="color: #808080;">&#41;</span>, <span style="color: #000;">14</span><span style="color: #808080;">&#41;</span>,<span style="color: #000;">8</span><span style="color: #808080;">&#41;</span>
<span style="color: #808080;">+</span><span style="color: #FF0000;">' '</span><span style="color: #808080;">+</span><span style="color: #0000FF;">RIGHT</span><span style="color: #808080;">&#40;</span><span style="color: #0000FF;">CONVERT</span><span style="color: #808080;">&#40;</span><span style="color: #0000FF;">NVARCHAR</span>, DateFirstPurchase, <span style="color: #000;">109</span><span style="color: #808080;">&#41;</span>, <span style="color: #000;">2</span><span style="color: #808080;">&#41;</span>
,N<span style="color: #FF0000;">''</span><span style="color: #808080;">&#41;</span>
<span style="color: #808080;">+</span>IS<span style="color: #808080;">NULL</span><span style="color: #808080;">&#40;</span><span style="color: #0000FF;">CAST</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#91;</span>CommuteDistance<span style="color: #808080;">&#93;</span> <span style="color: #0000FF;">AS</span> <span style="color: #0000FF;">NVARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">255</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span>,N<span style="color: #FF0000;">''</span><span style="color: #808080;">&#41;</span>
<span style="color: #808080;">&#41;</span>,<span style="color: #000;">1</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> <span style="color: #808080;">&#91;</span>Hashbytes<span style="color: #808080;">&#93;</span>
<span style="color: #0000FF;">FROM</span> DimCustomer</pre></div></div>

<p><em>And the results:</em><br />
<a href="http://timlaqua.com/wp-content/uploads/2012/02/HASHBYTES-Results.png"><img src="http://timlaqua.com/wp-content/uploads/2012/02/HASHBYTES-Results.png" alt="" title="HASHBYTES Results" width="720" height="169" class="alignnone size-full wp-image-960" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://timlaqua.com/2012/02/slowly-changing-dimensions-with-md5-hashes-in-ssis/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>SSAS Cache Isn&#8217;t Making Cents</title>
		<link>http://timlaqua.com/2012/02/ssas-cache-isnt-making-cents/</link>
		<comments>http://timlaqua.com/2012/02/ssas-cache-isnt-making-cents/#comments</comments>
		<pubDate>Fri, 03 Feb 2012 23:12:27 +0000</pubDate>
		<dc:creator>Tim</dc:creator>
				<category><![CDATA[Scripts & Code]]></category>
		<category><![CDATA[analysis services]]></category>
		<category><![CDATA[business intelligence]]></category>
		<category><![CDATA[ssas]]></category>

		<guid isPermaLink="false">http://timlaqua.com/?p=888</guid>
		<description><![CDATA[I stole the pun from my Business Analyst, Mr. John Seiler Now on to my issue - when SSAS caches the value for my [Actual] measure, it seems to do so based on the results of the first query that requests that coordinate. In this particular cube, there's bunches of tiny fractions and depending on [...]]]></description>
			<content:encoded><![CDATA[<p>I stole the pun from my Business Analyst, Mr. John Seiler <img src='http://timlaqua.com/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' />   Now on to my issue - when SSAS caches the value for my [Actual] measure, it seems to do so based on the results of the first query that requests that coordinate.  In this particular cube, there's bunches of tiny fractions and depending on how you slice it, it aggregates a little different.  This is a fun problem in itself, but the part that drives me (and the Finance department) crazy is that if you go and slice on something OTHER than that first query that created the cache, the values they see don't always add up to the "Grand Total" in Excel - aka. "All"</p>
<p>These are the queries used for this test:</p>
<p>Query A</p>

<div class="wp_syntax"><div class="code"><pre class="mdx" style="font-family:monospace;"><span style="color: #0000FF; font-weight: bold;">SELECT</span> 
	<span style="color: #003300; font-weight: bold;">&#123;</span>Actual<span style="color: #003300; font-weight: bold;">&#125;</span> <span style="color: #0000FF; font-weight: bold;">ON</span> <span style="color: #cc66cc;">0</span>
<span style="color: #0000FF; font-weight: bold;">FROM</span> <span style="color: #333333;">[GL]</span> 
CELL <span style="color: #FF00FF;">PROPERTIES</span> <span style="color: #CC00FF;">VALUE</span></pre></div></div>

<p>Query B</p>

<div class="wp_syntax"><div class="code"><pre class="mdx" style="font-family:monospace;"><span style="color: #0000FF; font-weight: bold;">SELECT</span> 
	<span style="color: #003300; font-weight: bold;">&#123;</span>Actual<span style="color: #003300; font-weight: bold;">&#125;</span> <span style="color: #0000FF; font-weight: bold;">ON</span> <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">,</span>
	<span style="color: #0000FF; font-weight: bold;">NON</span> <span style="color: #0000FF; font-weight: bold;">EMPTY</span> <span style="color: #0000FF;">Hierarchize</span><span style="color: #003300; font-weight: bold;">&#40;</span><span style="color: #003300; font-weight: bold;">&#123;</span><span style="color: #0000FF;">DrilldownLevel</span><span style="color: #003300; font-weight: bold;">&#40;</span><span style="color: #003300; font-weight: bold;">&#123;</span><span style="color: #333333;">[Account]</span>.<span style="color: #333333;">[Accounts]</span>.<span style="color: #333333;">[All]</span><span style="color: #003300; font-weight: bold;">&#125;</span><span style="color: #66cc66;">,,,</span>INCLUDE_CALC_MEMBERS<span style="color: #003300; font-weight: bold;">&#41;</span><span style="color: #003300; font-weight: bold;">&#125;</span><span style="color: #003300; font-weight: bold;">&#41;</span> <span style="color: #0000FF; font-weight: bold;">ON</span> <span style="color: #cc66cc;">1</span>
<span style="color: #0000FF; font-weight: bold;">FROM</span> <span style="color: #333333;">[GL]</span> 
CELL <span style="color: #FF00FF;">PROPERTIES</span> <span style="color: #CC00FF;">VALUE</span></pre></div></div>

<p>Query C</p>

<div class="wp_syntax"><div class="code"><pre class="mdx" style="font-family:monospace;"><span style="color: #0000FF; font-weight: bold;">SELECT</span> 
	<span style="color: #003300; font-weight: bold;">&#123;</span>Actual<span style="color: #003300; font-weight: bold;">&#125;</span> <span style="color: #0000FF; font-weight: bold;">ON</span> <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">,</span>
	<span style="color: #0000FF; font-weight: bold;">NON</span> <span style="color: #0000FF; font-weight: bold;">EMPTY</span> <span style="color: #0000FF;">Hierarchize</span><span style="color: #003300; font-weight: bold;">&#40;</span><span style="color: #003300; font-weight: bold;">&#123;</span><span style="color: #0000FF;">DrilldownLevel</span><span style="color: #003300; font-weight: bold;">&#40;</span><span style="color: #003300; font-weight: bold;">&#123;</span><span style="color: #333333;">[Date]</span>.<span style="color: #333333;">[Fiscal]</span>.<span style="color: #333333;">[All]</span><span style="color: #003300; font-weight: bold;">&#125;</span><span style="color: #66cc66;">,,,</span>INCLUDE_CALC_MEMBERS<span style="color: #003300; font-weight: bold;">&#41;</span><span style="color: #003300; font-weight: bold;">&#125;</span><span style="color: #003300; font-weight: bold;">&#41;</span> <span style="color: #0000FF; font-weight: bold;">ON</span> <span style="color: #cc66cc;">1</span>
<span style="color: #0000FF; font-weight: bold;">FROM</span> <span style="color: #333333;">[GL]</span> 
CELL <span style="color: #FF00FF;">PROPERTIES</span> <span style="color: #CC00FF;">VALUE</span>
GO</pre></div></div>

<style>
td {vertical-align: top;}
</style>
<p>Results (Cache was cleared with ClearCache on the entire Database before each Series)</p>
<table cellpadding=0 cellspacing=0>
<tr>
<th>Series 1</th>
<th>Series 2</th>
<th>Series 3</th>
</tr>
<tr>
<td>
<a href="http://timlaqua.com/wp-content/uploads/2012/02/Series-1-Query-A.png"><img src="http://timlaqua.com/wp-content/uploads/2012/02/Series-1-Query-A.png" alt="" title="Series 1 - Query A" width="134" height="41" class="alignnone size-full wp-image-890" /></a>
  </td>
<td>
<a href="http://timlaqua.com/wp-content/uploads/2012/02/Series-2-Query-B.png"><img src="http://timlaqua.com/wp-content/uploads/2012/02/Series-2-Query-B.png" alt="" title="Series 2 - Query B" width="182" height="126" class="alignnone size-full wp-image-894" /></a>
  </td>
<td>
<a href="http://timlaqua.com/wp-content/uploads/2012/02/Series-3-Query-C.png"><img src="http://timlaqua.com/wp-content/uploads/2012/02/Series-3-Query-C.png" alt="" title="Series 3 - Query C" width="177" height="115" class="alignnone size-full wp-image-898" /></a>
  </td>
</tr>
<tr>
<td>
<a href="http://timlaqua.com/wp-content/uploads/2012/02/Series-1-Query-B.png"><img src="http://timlaqua.com/wp-content/uploads/2012/02/Series-1-Query-B.png" alt="" title="Series 1 - Query B" width="181" height="124" class="alignnone size-full wp-image-891" /></a>
  </td>
<td>
<a href="http://timlaqua.com/wp-content/uploads/2012/02/Series-2-Query-C.png"><img src="http://timlaqua.com/wp-content/uploads/2012/02/Series-2-Query-C.png" alt="" title="Series 2 - Query C" width="179" height="117" class="alignnone size-full wp-image-895" /></a>
  </td>
<td>
<a href="http://timlaqua.com/wp-content/uploads/2012/02/Series-3-Query-A.png"><img src="http://timlaqua.com/wp-content/uploads/2012/02/Series-3-Query-A.png" alt="" title="Series 3 - Query A" width="132" height="38" class="alignnone size-full wp-image-896" /></a>
  </td>
<tr>
<tr>
<td>
<a href="http://timlaqua.com/wp-content/uploads/2012/02/Series-1-Query-C.png"><img src="http://timlaqua.com/wp-content/uploads/2012/02/Series-1-Query-C.png" alt="" title="Series 1 - Query C" width="176" height="116" class="alignnone size-full wp-image-892" /></a>
  </td>
<td>
<a href="http://timlaqua.com/wp-content/uploads/2012/02/Series-2-Query-A.png"><img src="http://timlaqua.com/wp-content/uploads/2012/02/Series-2-Query-A.png" alt="" title="Series 2 - Query A" width="136" height="39" class="alignnone size-full wp-image-893" /></a>
  </td>
<td>
<a href="http://timlaqua.com/wp-content/uploads/2012/02/Series-3-Query-B.png"><img src="http://timlaqua.com/wp-content/uploads/2012/02/Series-3-Query-B.png" alt="" title="Series 3 - Query B" width="182" height="143" class="alignnone size-full wp-image-897" /></a>
  </td>
<tr>
</table>
<p>So basically the Grand Total of this GL cube is a random number based on whoever sneaks the first query in after the cache is cleared (processing, sync, etc).</p>
<p>And for all of you that think the MDX script breaks everything everywhere - I did comment out my entire MDX script before running these tests.</p>
]]></content:encoded>
			<wfw:commentRss>http://timlaqua.com/2012/02/ssas-cache-isnt-making-cents/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Getting a useful FTP error message out of SSIS</title>
		<link>http://timlaqua.com/2010/10/getting-a-useful-ftp-error-message-out-of-ssis/</link>
		<comments>http://timlaqua.com/2010/10/getting-a-useful-ftp-error-message-out-of-ssis/#comments</comments>
		<pubDate>Sat, 30 Oct 2010 14:10:55 +0000</pubDate>
		<dc:creator>Tim</dc:creator>
				<category><![CDATA[Scripts & Code]]></category>
		<category><![CDATA[bi]]></category>
		<category><![CDATA[business intelligence]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[ftp]]></category>
		<category><![CDATA[ssis]]></category>
		<category><![CDATA[ssis event handler]]></category>

		<guid isPermaLink="false">http://timlaqua.com/?p=385</guid>
		<description><![CDATA[The plan: Upload a zip file for a bunch of different clients to various user-specified ftp servers - looks something like this: A quick note on what that "Update FTP Connection" task is doing - it's modifying the properties of the "FTP" connection manager to the appropriate Server/Username/Password for this particular client: 1 2 3 [...]]]></description>
			<content:encoded><![CDATA[<p>The plan:  Upload a zip file for a bunch of different clients to various user-specified ftp servers - looks something like this:<br />
<a href="http://timlaqua.com/wp-content/uploads/2010/10/FTPErrors-ControlFlow.png"><img src="http://timlaqua.com/wp-content/uploads/2010/10/FTPErrors-ControlFlow.png" alt="" title="FTPErrors-ControlFlow" width="272" height="197" class="alignnone size-full wp-image-391" /></a></p>
<p><em>A quick note on what that "Update FTP Connection" task is doing - it's modifying the properties of the "FTP" connection manager to the appropriate Server/Username/Password for this particular client:</em></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;">        <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> Main<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            ConnectionManager ftp <span style="color: #008000;">=</span> Dts.<span style="color: #0000FF;">Connections</span><span style="color: #000000;">&#91;</span><span style="color: #666666;">&quot;FTP&quot;</span><span style="color: #000000;">&#93;</span><span style="color: #008000;">;</span>
            ftp.<span style="color: #0000FF;">Properties</span><span style="color: #000000;">&#91;</span><span style="color: #666666;">&quot;ServerName&quot;</span><span style="color: #000000;">&#93;</span>.<span style="color: #0000FF;">SetValue</span><span style="color: #000000;">&#40;</span>ftp, <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span><span style="color: #000000;">&#41;</span>Dts.<span style="color: #0000FF;">Variables</span><span style="color: #000000;">&#91;</span><span style="color: #666666;">&quot;FTPServer&quot;</span><span style="color: #000000;">&#93;</span>.<span style="color: #0000FF;">Value</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            ftp.<span style="color: #0000FF;">Properties</span><span style="color: #000000;">&#91;</span><span style="color: #666666;">&quot;ServerUserName&quot;</span><span style="color: #000000;">&#93;</span>.<span style="color: #0000FF;">SetValue</span><span style="color: #000000;">&#40;</span>ftp, <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span><span style="color: #000000;">&#41;</span>Dts.<span style="color: #0000FF;">Variables</span><span style="color: #000000;">&#91;</span><span style="color: #666666;">&quot;FTPUser&quot;</span><span style="color: #000000;">&#93;</span>.<span style="color: #0000FF;">Value</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            ftp.<span style="color: #0000FF;">Properties</span><span style="color: #000000;">&#91;</span><span style="color: #666666;">&quot;ServerPassword&quot;</span><span style="color: #000000;">&#93;</span>.<span style="color: #0000FF;">SetValue</span><span style="color: #000000;">&#40;</span>ftp, <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span><span style="color: #000000;">&#41;</span>Dts.<span style="color: #0000FF;">Variables</span><span style="color: #000000;">&#91;</span><span style="color: #666666;">&quot;User::FTPPassword&quot;</span><span style="color: #000000;">&#93;</span>.<span style="color: #0000FF;">Value</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
            Dts.<span style="color: #0000FF;">TaskResult</span> <span style="color: #008000;">=</span> <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span><span style="color: #000000;">&#41;</span>ScriptResults.<span style="color: #0000FF;">Success</span><span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>The problem: Anyone who has tried to upload multiple files to multiple FTP sites in an SSIS package very quickly ran in to FTP errors (that's what happens when you let users tell you what their FTP url and authentication is).  You can handle these errors and log them via the standard OnError handler dumping the ErrorCode, ErrorDescription, and usually SourceName out to a flat file or table.<br />
<span id="more-385"></span></p>
<p>Now, when you look at the ErrorDescription for an FTP Task - you'll be disappointed.  It usually states "Unable to connect to FTP server..." or something to that effect.  Looking at the execution results in the progress tab, you'll see that the FTP Connection Manager threw a more detailed description of the error:<br />
<a href="http://timlaqua.com/wp-content/uploads/2010/10/FTPErrors-ProgressScreen.png"><img src="http://timlaqua.com/wp-content/uploads/2010/10/FTPErrors-ProgressScreen-300x90.png" alt="" title="FTPErrors-ProgressScreen" width="300" height="90" class="alignnone size-medium wp-image-395" /></a></p>
<p>Good luck getting at that detailed error description - My first attempt here was to acquire a connection and connect via the connection manager and trap the error that came back.  While that worked, the error that was trapped was HRESULT 0xC001602A, which basically means that something bad happened.</p>
<p>Now we move on to phase two:  Retry the FTP operation via .NET FtpWebRequest and trap that error.  This can either flat out replace the FTP Task as a script task or, not sure why I did it this way - I suppose I like to try and use SSIS built in tasks as much as possible - you can retry the operation in the OnError handler assuming you have the requisite information (url, username, and password) available.</p>
<p>Create the OnError handler (either at the package level for everything or just on the executable you care about - I chose the latter):<br />
<a href="http://timlaqua.com/wp-content/uploads/2010/10/FTPErrors-CreateOnError.png"><img src="http://timlaqua.com/wp-content/uploads/2010/10/FTPErrors-CreateOnError.png" alt="" title="FTPErrors-CreateOnError" width="212" height="94" class="alignnone size-full wp-image-392" /></a></p>
<p>Then add a data flow task - inside that add a Script Component (as a source) and an appropriate destination (Here it's going to an OleDb destination):<br />
<a href="http://timlaqua.com/wp-content/uploads/2010/10/FTPErrors-OnErrorDataFlowTasks.png"><img src="http://timlaqua.com/wp-content/uploads/2010/10/FTPErrors-OnErrorDataFlowTasks.png" alt="" title="FTPErrors-OnErrorDataFlowTasks" width="175" height="164" class="alignnone size-full wp-image-394" /></a></p>
<p>Configure the Script Component for ReadOnly access to the requisite variables:<br />
<a href="http://timlaqua.com/wp-content/uploads/2010/10/FTPErrors-ScriptComponentVars.png"><img src="http://timlaqua.com/wp-content/uploads/2010/10/FTPErrors-ScriptComponentVars-300x236.png" alt="" title="FTPErrors-ScriptComponentVars" width="300" height="236" class="alignnone size-medium wp-image-397" /></a></p>
<p>And configure the Output buffer:<br />
<a href="http://timlaqua.com/wp-content/uploads/2010/10/FTPErrors-ScriptComponentOutput.png"><img src="http://timlaqua.com/wp-content/uploads/2010/10/FTPErrors-ScriptComponentOutput-300x173.png" alt="" title="FTPErrors-ScriptComponentOutput" width="300" height="173" class="alignnone size-medium wp-image-396" /></a></p>
<p>Now go edit the actual script:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">using</span> <span style="color: #008080;">System</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Data</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">Microsoft.SqlServer.Dts.Pipeline.Wrapper</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">Microsoft.SqlServer.Dts.Runtime.Wrapper</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Net</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.IO</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Text</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #000000;">&#91;</span>Microsoft.<span style="color: #0000FF;">SqlServer</span>.<span style="color: #0000FF;">Dts</span>.<span style="color: #0000FF;">Pipeline</span>.<span style="color: #0000FF;">SSISScriptComponentEntryPointAttribute</span><span style="color: #000000;">&#93;</span>
<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> ScriptMain <span style="color: #008000;">:</span> UserComponent
<span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">override</span> <span style="color: #0600FF;">void</span> CreateNewOutputRows<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        <span style="color: #FF0000;">string</span> errorDescription <span style="color: #008000;">=</span> Variables.<span style="color: #0000FF;">ErrorDescription</span><span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>Variables.<span style="color: #0000FF;">ErrorDescription</span>.<span style="color: #0000FF;">Contains</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Unable to connect to FTP server&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #0600FF;">try</span>
            <span style="color: #000000;">&#123;</span>
                FtpWebRequest ftp <span style="color: #008000;">=</span> <span style="color: #000000;">&#40;</span>FtpWebRequest<span style="color: #000000;">&#41;</span>WebRequest.<span style="color: #0000FF;">Create</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;ftp://&quot;</span> <span style="color: #008000;">+</span> Variables.<span style="color: #0000FF;">FTPUrl</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                ftp.<span style="color: #0000FF;">Method</span> <span style="color: #008000;">=</span> WebRequestMethods.<span style="color: #0000FF;">Ftp</span>.<span style="color: #0000FF;">UploadFile</span><span style="color: #008000;">;</span>
                ftp.<span style="color: #0000FF;">Credentials</span> <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> <span style="color: #000000;">System.<span style="color: #0000FF;">Net</span></span>.<span style="color: #0000FF;">NetworkCredential</span><span style="color: #000000;">&#40;</span>Variables.<span style="color: #0000FF;">FTPUser</span>, Variables.<span style="color: #0000FF;">FTPPassword</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
                StreamReader sourceStream <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> StreamReader<span style="color: #000000;">&#40;</span>Variables.<span style="color: #0000FF;">FTPLocalPath</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                <span style="color: #FF0000;">byte</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> fileContents <span style="color: #008000;">=</span> Encoding.<span style="color: #0000FF;">UTF8</span>.<span style="color: #0000FF;">GetBytes</span><span style="color: #000000;">&#40;</span>sourceStream.<span style="color: #0000FF;">ReadToEnd</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                sourceStream.<span style="color: #0000FF;">Close</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                ftp.<span style="color: #0000FF;">ContentLength</span> <span style="color: #008000;">=</span> fileContents.<span style="color: #0000FF;">Length</span><span style="color: #008000;">;</span>
&nbsp;
                Stream requestStream <span style="color: #008000;">=</span> ftp.<span style="color: #0000FF;">GetRequestStream</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                requestStream.<span style="color: #0000FF;">Write</span><span style="color: #000000;">&#40;</span>fileContents, <span style="color: #FF0000;">0</span>, fileContents.<span style="color: #0000FF;">Length</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                requestStream.<span style="color: #0000FF;">Close</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
                FtpWebResponse response <span style="color: #008000;">=</span> <span style="color: #000000;">&#40;</span>FtpWebResponse<span style="color: #000000;">&#41;</span>ftp.<span style="color: #0000FF;">GetResponse</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
                response.<span style="color: #0000FF;">Close</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
                errorDescription <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;Upload retry attempt via .NET was successful.&quot;</span><span style="color: #008000;">;</span>
            <span style="color: #000000;">&#125;</span>
            <span style="color: #0600FF;">catch</span> <span style="color: #000000;">&#40;</span>Exception e<span style="color: #000000;">&#41;</span>
            <span style="color: #000000;">&#123;</span>
                errorDescription <span style="color: #008000;">=</span> e.<span style="color: #0000FF;">Message</span><span style="color: #008000;">;</span>
            <span style="color: #000000;">&#125;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
&nbsp;
        ErrorSourceBuffer.<span style="color: #0000FF;">AddRow</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        ErrorSourceBuffer.<span style="color: #0000FF;">ErrorCode</span> <span style="color: #008000;">=</span> Variables.<span style="color: #0000FF;">ErrorCode</span>.<span style="color: #0000FF;">ToString</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        ErrorSourceBuffer.<span style="color: #0000FF;">ErrorDescription</span> <span style="color: #008000;">=</span> errorDescription<span style="color: #008000;">;</span>
        ErrorSourceBuffer.<span style="color: #0000FF;">ErrorSource</span> <span style="color: #008000;">=</span> Variables.<span style="color: #0000FF;">SourceName</span><span style="color: #008000;">;</span>
        ErrorSourceBuffer.<span style="color: #0000FF;">FTPUrl</span> <span style="color: #008000;">=</span> Variables.<span style="color: #0000FF;">FTPUrl</span><span style="color: #008000;">;</span>
        ErrorSourceBuffer.<span style="color: #0000FF;">FTPUser</span> <span style="color: #008000;">=</span> Variables.<span style="color: #0000FF;">FTPUser</span><span style="color: #008000;">;</span>
        ErrorSourceBuffer.<span style="color: #0000FF;">FTPPassword</span> <span style="color: #008000;">=</span> Variables.<span style="color: #0000FF;">FTPPassword</span><span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>And bob's your uncle.  Now you have meaningful text in the ErrorDescription column of the output buffer instead of some vague "something's wrong" message.</p>
<p>Here's a flat file destination walkthrough for the OnError handler: <a href="http://vsteamsystemcentral.com/cs21/blogs/applied_business_intelligence/archive/2007/04/08/ssis-design-pattern-custom-error-handling.aspx">http://vsteamsystemcentral.com/cs21/blogs/applied_business_intelligence/archive/2007/04/08/ssis-design-pattern-custom-error-handling.aspx</a></p>
]]></content:encoded>
			<wfw:commentRss>http://timlaqua.com/2010/10/getting-a-useful-ftp-error-message-out-of-ssis/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Excel 2007 Hangs When Trying to Edit an OLAP Cube Filter</title>
		<link>http://timlaqua.com/2010/04/excel-2007-hangs-when-trying-to-edit-an-olap-cube-filter/</link>
		<comments>http://timlaqua.com/2010/04/excel-2007-hangs-when-trying-to-edit-an-olap-cube-filter/#comments</comments>
		<pubDate>Wed, 07 Apr 2010 03:38:16 +0000</pubDate>
		<dc:creator>Tim</dc:creator>
				<category><![CDATA[Scripts & Code]]></category>
		<category><![CDATA[analysis services]]></category>
		<category><![CDATA[bi]]></category>
		<category><![CDATA[business intelligence]]></category>
		<category><![CDATA[excel 2007]]></category>
		<category><![CDATA[pivot table]]></category>
		<category><![CDATA[ssas]]></category>
		<category><![CDATA[xmla]]></category>

		<guid isPermaLink="false">http://timlaqua.com/?p=371</guid>
		<description><![CDATA[Ok, it doesn't hang every time you try to edit an OLAP cube filter, but sometimes - it appears to. In reality, I've never seen it permanently hang - just kind of go away for a while. Here's the basic symptom that the business will report to you: "Excel freezes (or hangs) when I try [...]]]></description>
			<content:encoded><![CDATA[<p>Ok, it doesn't hang every time you try to edit an OLAP cube filter, but sometimes - it appears to.  In reality, I've never seen it permanently hang - just kind of go away for a while.  Here's the basic symptom that the business will report to you:</p>
<p><em>"Excel freezes (or hangs) when I try to change this filter"</em><br />
<a href="http://timlaqua.com/wp-content/uploads/2010/04/DeliveryDate-Date-Broken.png"><img src="http://timlaqua.com/wp-content/uploads/2010/04/DeliveryDate-Date-Broken.png" alt="" title="DeliveryDate-Date-Broken" width="352" height="351" class="alignnone size-full wp-image-373" /></a></p>
<p>Most of us have seen this at one point or another and shrugged it off as a busy time or processing is going on or there are cats clogging up the tubes, etc.  Tonight, I finally decided to figure out what's causing it.<br />
<span id="more-371"></span><br />
When you have an OLAP cube filter in your pivot table and expand the items list to select your filter items, Excel fires a bunch of Discover commands at the cube (these are pretty light in most cases).  If you don't have anything selected yet (so you just have the default member selected) Excel isn't doing much work here and the list pops right up.  Here's two basic flow examples for what discover commands are fired off when you DO have an item selected:</p>
<p>If you have one level 1 member selected</p>
<ol>
<li>MDSCHEMA_CUBES (whatever cube you're connected to)</li>
<li>MDSCHEMA_CUBES (again, same thing)</li>
<li>MDSCHEMA_MEMBERS (LEVEL_UNIQUE_NAME of the filter), no TREE_OP</li>
<li>MDSCHEMA_MEMBERS (MEMBER_UNIQUE_NAME of the selected member) TREE_OP 4, only immediate parent</li>
<li>MDSCHEMA_MEMBERS (MEMBER_UNIQUE_NAME of the selected member's parent) TREE_OP 8, returns itself</li>
<li>MDSCHEMA_MEMBERS (MEMBER_UNIQUE_NAME of the selected member's parent) TREE_OP 1, returns all children</li>
</ol>
<p>If you have one level 2 member selected in a hierarchy</p>
<ol>
<li>MDSCHEMA_CUBES (whatever cube you're connected to)</li>
<li>MDSCHEMA_CUBES (again, same thing)</li>
<li>MDSCHEMA_MEMBERS (LEVEL_UNIQUE_NAME of the filter), no TREE_OP</li>
<li>MDSCHEMA_MEMBERS (MEMBER_UNIQUE_NAME of the selected member) TREE_OP 4, only immediate parent</li>
<li>MDSCHEMA_MEMBERS (MEMBER_UNIQUE_NAME of the selected member's parent's parent)  TREE_OP 4, immediate parent</li>
<li>MDSCHEMA_MEMBERS (MEMBER_UNIQUE_NAME of the selected member's parent's parent) TREE_OP 8, returns itself</li>
<li>MDSCHEMA_MEMBERS (MEMBER_UNIQUE_NAME of the selected member's parent's parent)  TREE_OP 1, all children</li>
<li>MDSCHEMA_MEMBERS (MEMBER_UNIQUE_NAME of the selected member's parent) TREE_OP 8, returns itself</li>
<li>MDSCHEMA_MEMBERS (MEMBER_UNIQUE_NAME of the selected member's parent) TREE_OP 1, returns all children</li>
</ol>
<p>So, that's six requests for a level 1 member and 9 requests for a level 2 member - both of these take milliseconds to complete the whole flow, so no big deal.  When does this become a big deal?  When you do something like click the "Select multiple items" checkbox, expand a hierarchy and UNCHECK something... like if you opened the Adventure Works 2008 cube, added Delivery Date.Date as a filter, expanded the "All Periods" level and deselected the first day:<br />
<a href="http://timlaqua.com/wp-content/uploads/2010/04/DeliveryDate-Date-Setup.png"><img src="http://timlaqua.com/wp-content/uploads/2010/04/DeliveryDate-Date-Setup.png" alt="" title="DeliveryDate-Date-Setup" width="312" height="418" class="alignnone size-full wp-image-372" /></a></p>
<p>Now click OK and expand the items box - we get a familiar popup:<br />
<a href="http://timlaqua.com/wp-content/uploads/2010/04/DeliveryDate-Date-Broken.png"><img src="http://timlaqua.com/wp-content/uploads/2010/04/DeliveryDate-Date-Broken.png" alt="" title="DeliveryDate-Date-Broken" width="352" height="351" class="alignnone size-full wp-image-373" /></a></p>
<p>It comes back after a few seconds with the expected item list, but why did it take so long?  Well, based on our flows above, we have a whole bunch of level 1 members selected - 1187 of them to be exact.  That means there will be 1187 requests for their parent member (TREE_OP 4) which will all return the [All Periods] member.  To illustrate this, I traced that exact operation:<br />
<a href="http://timlaqua.com/wp-content/uploads/2010/04/DeliveryDate-Date-Profiler.png"><img src="http://timlaqua.com/wp-content/uploads/2010/04/DeliveryDate-Date-Profiler-300x144.png" alt="" title="DeliveryDate-Date-Profiler" width="300" height="144" class="alignnone size-medium wp-image-374" /></a></p>
<p>The profiler captured 1192 discover requests - 1187 of them were getting the parents of selected members, two were MDSCHEMA_CUBES requests, one was getting [All Periods], one was getting all the children of [All Periods], and one was getting the Unique Name of the root level.  Not cool.  Imagine what happens when your talking about tens of thousands of members at a particular level!  Granted, that seems unlikely but understand that the logic behind this is "I don't want to see this", "hide just this member", and so forth - in some cases people have no idea what actually happened was they unchecked one of 6,000 members.</p>
<p>So what to do about this?  Educate.  To fix the immediate problem of a seemingly broken Excel pivot table - just wait for the filter to load, it'll come back eventually.  In the future when working with Excel filters, always favor selecting vs. deselecting and filtering at higher levels in the hierarchy whenever possible.</p>
]]></content:encoded>
			<wfw:commentRss>http://timlaqua.com/2010/04/excel-2007-hangs-when-trying-to-edit-an-olap-cube-filter/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Clone Analysis Services Partitions with PowerShell</title>
		<link>http://timlaqua.com/2010/01/clone-analysis-services-partitions-with-powershell/</link>
		<comments>http://timlaqua.com/2010/01/clone-analysis-services-partitions-with-powershell/#comments</comments>
		<pubDate>Fri, 15 Jan 2010 20:45:20 +0000</pubDate>
		<dc:creator>Tim</dc:creator>
				<category><![CDATA[Scripts & Code]]></category>
		<category><![CDATA[amo]]></category>
		<category><![CDATA[analysis services]]></category>
		<category><![CDATA[bi]]></category>
		<category><![CDATA[business intelligence]]></category>
		<category><![CDATA[sql]]></category>
		<category><![CDATA[t-sql]]></category>

		<guid isPermaLink="false">http://timlaqua.com/?p=322</guid>
		<description><![CDATA[Most of us with large Analysis Services cubes partition our cubes by month or year or some other time-based slice and we have all, at one point or another, developed some way to create partitions for new months on-demand. Often, the solution to this seems to be a C# console application or SSIS package using [...]]]></description>
			<content:encoded><![CDATA[<p>Most of us with large Analysis Services cubes partition our cubes by month or year or some other time-based slice and we have all, at one point or another, developed some way to create partitions for new months on-demand.  Often, the solution to this seems to be a C# console application or SSIS package using AMO to create a new partition based off an existing partition.  The problem I see with this is that maintaining it requires opening up the project or package, making changes, re-compiling, deploying, testing, deploying to production, verifying, etc.  It also requires that whoever is going to maintain it is comfortable with C#.</p>
<p>To simplify the maintenance and get rid of the "black box" factor that utility apps like this tend to have, I put together a PowerShell script to do the same thing and a stored procedure to call the script.  Really, it doesn't matter what you use as you're most likely using an almost identical chunk of code to get your new partition created - my argument is that using PowerShell instead of C# or SSIS reduces the cost of maintenance, improves readability, and facilitates better understanding throughout your team.<br />
<span id="more-322"></span><br />
<em>The PowerShell Script: cloneSSASPartition.ps1</em></p>

<div class="wp_syntax"><div class="code"><pre class="powershell" style="font-family:monospace;"><span style="color: #0000FF;">param</span><span style="color: #000000;">&#40;</span>
   <span style="color: #000000;">&#91;</span><span style="color: #008080;">string</span><span style="color: #000000;">&#93;</span> <span style="color: #800080;">$ServerName</span><span style="color: pink;">,</span> 		<span style="color: pink;">&lt;</span><span style="color: #008000;"># The name of the Analysis Services Instance #&gt;</span>
   <span style="color: #000000;">&#91;</span><span style="color: #008080;">string</span><span style="color: #000000;">&#93;</span> <span style="color: #800080;">$DatabaseName</span><span style="color: pink;">,</span> 		<span style="color: pink;">&lt;</span><span style="color: #008000;"># The name of the Database our Cube is in #&gt;</span>
   <span style="color: #000000;">&#91;</span><span style="color: #008080;">string</span><span style="color: #000000;">&#93;</span> <span style="color: #800080;">$CubeName</span><span style="color: pink;">,</span>     		<span style="color: pink;">&lt;</span><span style="color: #008000;"># The name of the Cube that our Measure Group is in #&gt;</span>
   <span style="color: #000000;">&#91;</span><span style="color: #008080;">string</span><span style="color: #000000;">&#93;</span> <span style="color: #800080;">$MeasureGroupName</span><span style="color: pink;">,</span> 	<span style="color: pink;">&lt;</span><span style="color: #008000;"># The name of the Measure Group that our Partition is in #&gt;</span>
   <span style="color: #000000;">&#91;</span><span style="color: #008080;">string</span><span style="color: #000000;">&#93;</span> <span style="color: #800080;">$SourcePartitionName</span><span style="color: pink;">,</span> 	<span style="color: pink;">&lt;</span><span style="color: #008000;"># The name of the Partition we want to merge in to another partition #&gt;</span>
   <span style="color: #000000;">&#91;</span><span style="color: #008080;">string</span><span style="color: #000000;">&#93;</span> <span style="color: #800080;">$NewPartitionName</span><span style="color: pink;">,</span> 	<span style="color: pink;">&lt;</span><span style="color: #008000;"># The desired name of the new partition #&gt;</span>
   <span style="color: #000000;">&#91;</span><span style="color: #008080;">string</span><span style="color: #000000;">&#93;</span> <span style="color: #800080;">$NewPartitionQuery</span> 	<span style="color: pink;">&lt;</span><span style="color: #008000;"># The Query source for the new partition (will use the same datasource as the SourcePartition #&gt;</span>
<span style="color: #000000;">&#41;</span>
&nbsp;
<span style="color: #000000;">&#91;</span><span style="color: #008080;">System.Reflection.Assembly</span><span style="color: #000000;">&#93;</span>::<span style="color: #800000;">LoadWithPartialName</span><span style="color: #000000;">&#40;</span><span style="color: #800000;">&quot;Microsoft.AnalysisServices&quot;</span><span style="color: #000000;">&#41;</span> <span style="color: pink;">&gt;</span><span style="color: #800080;">$NULL</span>
&nbsp;
<span style="color: #800080;">$server</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">New-Object</span> Microsoft.AnalysisServices.Server
<span style="color: #800080;">$server</span>.Connect<span style="color: #000000;">&#40;</span><span style="color: #800080;">$ServerName</span><span style="color: #000000;">&#41;</span>
&nbsp;
<span style="color: #800080;">$database</span> <span style="color: pink;">=</span> <span style="color: #800080;">$server</span>.Databases.GetByName<span style="color: #000000;">&#40;</span><span style="color: #800080;">$DatabaseName</span><span style="color: #000000;">&#41;</span>
<span style="color: #800080;">$cube</span> <span style="color: pink;">=</span> <span style="color: #800080;">$database</span>.Cubes.GetByName<span style="color: #000000;">&#40;</span><span style="color: #800080;">$CubeName</span><span style="color: #000000;">&#41;</span>
<span style="color: #800080;">$measureGroup</span> <span style="color: pink;">=</span> <span style="color: #800080;">$cube</span>.MeasureGroups.FindByName<span style="color: #000000;">&#40;</span><span style="color: #800080;">$MeasureGroupName</span><span style="color: #000000;">&#41;</span>
<span style="color: #800080;">$sourcePartition</span> <span style="color: pink;">=</span> <span style="color: #800080;">$measureGroup</span>.Partitions.GetByName<span style="color: #000000;">&#40;</span><span style="color: #800080;">$SourcePartitionName</span><span style="color: #000000;">&#41;</span>
<span style="color: #800080;">$sourceQueryBinding</span> <span style="color: pink;">=</span> <span style="color: #800080;">$sourcePartition</span>.Source
&nbsp;
<span style="color: #800080;">$newPartition</span> <span style="color: pink;">=</span> <span style="color: #800080;">$sourcePartition</span>.Clone<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
<span style="color: #800080;">$newPartition</span>.ID <span style="color: pink;">=</span> <span style="color: #800080;">$NewPartitionName</span>
<span style="color: #800080;">$newPartition</span>.Name <span style="color: pink;">=</span> <span style="color: #800080;">$NewPartitionName</span>
<span style="color: #800080;">$measureGroup</span>.Partitions.Add<span style="color: #000000;">&#40;</span><span style="color: #800080;">$newPartition</span><span style="color: #000000;">&#41;</span> <span style="color: pink;">&gt;</span> <span style="color: #800080;">$NULL</span>
<span style="color: #800080;">$newPartition</span>.Source <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">New-Object</span> Microsoft.AnalysisServices.QueryBinding<span style="color: #000000;">&#40;</span><span style="color: #800080;">$sourceQueryBinding</span>.DataSourceID<span style="color: pink;">,</span> <span style="color: #800080;">$NewPartitionQuery</span><span style="color: #000000;">&#41;</span>
<span style="color: #800080;">$newPartition</span>.Update<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
&nbsp;
<span style="color: #800080;">$server</span>.Disconnect<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span></pre></div></div>

<p><em>The stored procedure to call the script: [Utility_CloneSSASPartition_S01] - assumes you placed cloneSSASPartition.ps1 at the root of the C: drive.  If you put it elsewhere, update the proc to reflect the actual location.</em></p>

<div class="wp_syntax"><div class="code"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">SET</span> ANSI_<span style="color: #808080;">NULL</span>S <span style="color: #0000FF;">ON</span>
GO
&nbsp;
<span style="color: #0000FF;">SET</span> QUOTED_IDENTIFIER <span style="color: #0000FF;">ON</span>
GO
&nbsp;
<span style="color: #008080;">/*************************************************************************************
***
*** Procedure:  		[Utility_CloneSSASPartition]
*** Purpose:		Executes PowerShell script to clone a SSAS partition
***			
***			
*** Author:		tl
*** Date Created:		2010-01-15
*** 
*** Revision History
*** Date		Author			Description
*** 2010-01-15	tl			Created
*************************************************************************************/</span>
&nbsp;
<span style="color: #0000FF;">CREATE</span> <span style="color: #0000FF;">PROCEDURE</span> <span style="color: #808080;">&#91;</span>dbo<span style="color: #808080;">&#93;</span>.<span style="color: #808080;">&#91;</span>Utility_CloneSSASPartition_S01<span style="color: #808080;">&#93;</span>
<span style="color: #808080;">&#40;</span>
	@ServerName <span style="color: #0000FF;">AS</span> <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">255</span><span style="color: #808080;">&#41;</span>,
	@DatabaseName <span style="color: #0000FF;">AS</span> <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">255</span><span style="color: #808080;">&#41;</span>,
	@CubeName <span style="color: #0000FF;">AS</span> <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">255</span><span style="color: #808080;">&#41;</span>,
	@MeasureGroupName <span style="color: #0000FF;">AS</span> <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">255</span><span style="color: #808080;">&#41;</span>,
	@SourcePartitionName <span style="color: #0000FF;">AS</span> <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">255</span><span style="color: #808080;">&#41;</span>,
	@NewPartitionName <span style="color: #0000FF;">AS</span> <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">255</span><span style="color: #808080;">&#41;</span>,
	@NewPartitionQuery <span style="color: #0000FF;">AS</span> <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">255</span><span style="color: #808080;">&#41;</span>
<span style="color: #808080;">&#41;</span>
<span style="color: #0000FF;">AS</span> 
&nbsp;
<span style="color: #0000FF;">SET</span> <span style="color: #0000FF;">NOCOUNT</span> <span style="color: #0000FF;">ON</span>
<span style="color: #0000FF;">DECLARE</span> @cmd <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">8000</span><span style="color: #808080;">&#41;</span>
<span style="color: #0000FF;">SET</span> @cmd <span style="color: #808080;">=</span> 
	<span style="color: #FF0000;">'powershell.exe -Command &quot;&amp; {'</span>
	<span style="color: #808080;">+</span> <span style="color: #FF0000;">' C:<span style="color: #000099; font-weight: bold;">\c</span>loneSSASPartition.ps1'</span>
	<span style="color: #808080;">+</span> <span style="color: #FF0000;">' -ServerName:<span style="color: #000099; font-weight: bold;">\&quot;</span>'</span> <span style="color: #808080;">+</span> @ServerName <span style="color: #808080;">+</span> <span style="color: #FF0000;">'<span style="color: #000099; font-weight: bold;">\&quot;</span>'</span>
	<span style="color: #808080;">+</span> <span style="color: #FF0000;">' -DatabaseName:<span style="color: #000099; font-weight: bold;">\&quot;</span>'</span> <span style="color: #808080;">+</span> @DatabaseName <span style="color: #808080;">+</span> <span style="color: #FF0000;">'<span style="color: #000099; font-weight: bold;">\&quot;</span>'</span>
	<span style="color: #808080;">+</span> <span style="color: #FF0000;">' -CubeName:<span style="color: #000099; font-weight: bold;">\&quot;</span>'</span> <span style="color: #808080;">+</span> @CubeName <span style="color: #808080;">+</span> <span style="color: #FF0000;">'<span style="color: #000099; font-weight: bold;">\&quot;</span>'</span>
	<span style="color: #808080;">+</span> <span style="color: #FF0000;">' -MeasureGroupName:<span style="color: #000099; font-weight: bold;">\&quot;</span>'</span> <span style="color: #808080;">+</span> @MeasureGroupName <span style="color: #808080;">+</span> <span style="color: #FF0000;">'<span style="color: #000099; font-weight: bold;">\&quot;</span>'</span>
	<span style="color: #808080;">+</span> <span style="color: #FF0000;">' -SourcePartitionName:<span style="color: #000099; font-weight: bold;">\&quot;</span>'</span> <span style="color: #808080;">+</span> @SourcePartitionName <span style="color: #808080;">+</span> <span style="color: #FF0000;">'<span style="color: #000099; font-weight: bold;">\&quot;</span>'</span>
	<span style="color: #808080;">+</span> <span style="color: #FF0000;">' -NewPartitionName:<span style="color: #000099; font-weight: bold;">\&quot;</span>'</span> <span style="color: #808080;">+</span> @NewPartitionName <span style="color: #808080;">+</span> <span style="color: #FF0000;">'<span style="color: #000099; font-weight: bold;">\&quot;</span>'</span>
	<span style="color: #808080;">+</span> <span style="color: #FF0000;">' -NewPartitionQuery:<span style="color: #000099; font-weight: bold;">\&quot;</span>'</span> <span style="color: #808080;">+</span> @NewPartitionQuery <span style="color: #808080;">+</span> <span style="color: #FF0000;">'<span style="color: #000099; font-weight: bold;">\&quot;</span>}&quot;'</span>
&nbsp;
<span style="color: #0000FF;">PRINT</span> @cmd
<span style="color: #0000FF;">EXEC</span> xp_cmdshell @cmd</pre></div></div>

<p>And, finally, we execute it</p>

<div class="wp_syntax"><div class="code"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">EXEC</span> <span style="color: #808080;">&#91;</span>dbo<span style="color: #808080;">&#93;</span>.<span style="color: #808080;">&#91;</span>Utility_CloneSSASPartition_S01<span style="color: #808080;">&#93;</span>
	 @ServerName <span style="color: #808080;">=</span> <span style="color: #FF0000;">'TESTSERVER'</span>
	,@DatabaseName <span style="color: #808080;">=</span> <span style="color: #FF0000;">'TestDatabase'</span>
	,@CubeName <span style="color: #808080;">=</span> <span style="color: #FF0000;">'TestCube'</span>
	,@MeasureGroupName <span style="color: #808080;">=</span> <span style="color: #FF0000;">'Test Measure Group'</span>
	,@SourcePartitionName <span style="color: #808080;">=</span> <span style="color: #FF0000;">'Test Partition - 200912'</span>
	,@NewPartitionName <span style="color: #808080;">=</span> <span style="color: #FF0000;">'Test Partition - 201001'</span>
	,@NewPartitionQuery <span style="color: #808080;">=</span> <span style="color: #FF0000;">'SELECT * FROM dbo.FactTableMonth('</span><span style="color: #FF0000;">'1/1/2010'</span><span style="color: #FF0000;">', '</span><span style="color: #FF0000;">'2/1/2010'</span><span style="color: #FF0000;">')'</span></pre></div></div>

<p>Again, I'm not saying there's anything "wrong" with using C# or SSIS Packages to do this sort of thing, I'm just pointing out that C# isn't always the answer - especially when someone other than you needs to maintain this thing in the future.  Keep it simple.</p>
]]></content:encoded>
			<wfw:commentRss>http://timlaqua.com/2010/01/clone-analysis-services-partitions-with-powershell/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Wouldn&#8217;t it be fun if Cubes could talk?</title>
		<link>http://timlaqua.com/2009/11/wouldnt-it-be-fun-if-cubes-could-talk/</link>
		<comments>http://timlaqua.com/2009/11/wouldnt-it-be-fun-if-cubes-could-talk/#comments</comments>
		<pubDate>Thu, 19 Nov 2009 19:47:21 +0000</pubDate>
		<dc:creator>Tim</dc:creator>
				<category><![CDATA[Scripts & Code]]></category>
		<category><![CDATA[Thoughts]]></category>
		<category><![CDATA[amo]]></category>
		<category><![CDATA[analysis services]]></category>
		<category><![CDATA[bi]]></category>
		<category><![CDATA[business intelligence]]></category>
		<category><![CDATA[cubes]]></category>

		<guid isPermaLink="false">http://timlaqua.com/?p=291</guid>
		<description><![CDATA[I didn't say "wouldn't it be useful" because after putting a test together, asking a cube questions with no context tends to return answers that it probably shouldn't have returned. In BI, it is incredibly important to understand what exactly it is you're asking for - if we just say we want "sales" and return [...]]]></description>
			<content:encoded><![CDATA[<p>I didn't say "wouldn't it be useful" because after putting a test together, asking a cube questions with no context tends to return answers that it probably shouldn't have returned.  In BI, it is incredibly important to understand what exactly it is you're asking for - if we just say we want "sales" and return an answer, nobody really knows what we meant by "sales."  Sure, in various circles, "sales" means the same thing - but once you start talking to different areas, departments, etc - the meaning of the word starts to shift.</p>
<p>But I digress - asking cubes questions is still pretty fun and some of the random things it returns when you point it at your own cubes can be flat out hilarious.</p>
<p>Here's a few questions thrown at the Adventure Works cube in the Adventure Works DW 2008 Analysis Services database<br />
<span id="more-291"></span></p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">what was sales for nichole nara
SELECT {[Measures].[Internet Sales Amount]} ON 0  FROM (SELECT {[Customer].[Customer].[Nichole Nara]} ON 0 FROM [Adventure Works])
$13,295.38
what were sales for nichole nara in cy 2004
SELECT {[Measures].[Internet Sales Amount]} ON 0  FROM (SELECT {[Date].[Calendar Year].[CY 2004]} ON 0 FROM (SELECT {[Customer].[Customer].[Nichole Nara]} ON 0 FROM [Adventure Works]))
$2,419.06
what were sales for nichole nara in cy 2004 in north america
SELECT {[Measures].[Internet Sales Amount]} ON 0  FROM (SELECT {[Sales Territory].[Sales Territory Group].[North America]} ON 0 FROM (SELECT {[Date].[Calendar Year].[CY 2004]} ON 0 FROM (SELECT {[Customer].[Customer].[Nichole Nara]} ON 0 FROM [Adventure Works])))
No Results Found
what were sales for nichole nara in cy 2004 for bikes
SELECT {[Measures].[Internet Sales Amount]} ON 0  FROM (SELECT {[Product].[Category].[Bikes]} ON 0 FROM (SELECT {[Date].[Calendar Year].[CY 2004]} ON 0 FROM (SELECT {[Customer].[Customer].[Nichole Nara]} ON 0 FROM [Adventure Works])))
$2,384.07
what were sales in north america in fy 2003 for bikes
SELECT {[Measures].[Internet Sales Amount]} ON 0  FROM (SELECT {[Product].[Category].[Bikes]} ON 0 FROM (SELECT {[Date].[Fiscal Year].[FY 2003]} ON 0 FROM (SELECT {[Sales Territory].[Sales Territory Group].[North America]} ON 0 FROM [Adventure Works])))
$1,739,306.95
sales for cy 2004 in cy q1, cy q2, and cy q4 in north america and canada
SELECT {[Measures].[Internet Sales Amount]} ON 0  FROM (SELECT {[Sales Territory].[Sales Territory Group].[North America],[Customer].[Country].[Canada]} ON 0 FROM (SELECT {[Date].[Calendar Quarter of Year].[CY Q1],[Date].[Calendar Quarter of Year].[CY Q2],[Date].[Calendar Quarter of Year].[CY Q4]} ON 0 FROM (SELECT {[Date].[Calendar Year].[CY 2004]} ON 0 FROM [Adventure Works])))
I don't know - Query Failure: Members belong to different hierarchies in the  function.
sales for cy 2004 in cy q1, cy q2, and cy q4 in north america
SELECT {[Measures].[Internet Sales Amount]} ON 0  FROM (SELECT {[Sales Territory].[Sales Territory Group].[North America]} ON 0 FROM (SELECT {[Date].[Calendar Quarter of Year].[CY Q1],[Date].[Calendar Quarter of Year].[CY Q2],[Date].[Calendar Quarter of Year].[CY Q4]} ON 0 FROM (SELECT {[Date].[Calendar Year].[CY 2004]} ON 0 FROM [Adventure Works])))
$3,967,371.16
sales between cy 2003 and cy 2004 for large resellers
SELECT {[Measures].[Internet Sales Amount]} ON 0  FROM (SELECT [Large Resellers] ON 0 FROM (SELECT {[Date].[Calendar Year].[CY 2003]:[Date].[Calendar Year].[CY2004]} ON 0 FROM [Adventure Works]))
$19,561,960.04
sales for top 50 customers in fy 2004 in fy q1 and fy q3
SELECT {[Measures].[Internet Sales Amount]} ON 0  FROM (SELECT {[Date].[Fiscal Quarter of Year].[FY Q1],[Date].[Fiscal Quarter of Year].[FY Q3]} ON 0 FROM (SELECT {[Date].[Fiscal Year].[FY 2004]} ON 0 FROM (SELECT [Top 50 Customers] ON 0 FROM [Adventure Works])))
$177,263.41</pre></div></div>

<p>The above questions returned pretty much exactly what I was looking for except for the one that threw us an error about members belonging to different hierarchies.  That one was because "Canada" was in [Customer].[Country] and "north america" was in [Sales Territory].[Sales Territory Group].  Yeah, it was a dumb question because canada is in north america, but it illustrates that when you don't qualify things, you can get some pretty confusing results.</p>
<p><strong>How's it work?</strong></p>
<ol>
<li>Enumerate all the Attribute Values for each Attribute Hierarchy and store them as "keywords"</li>
<li>Get some input</li>
<li>Look for the aforementioned "keywords" in the input string</li>
<li>Construct an MDX query based on what "keywords" were found in the string</li>
<li>Make sure there aren't any extra (non-noise) words in the string</li>
<li>Run Query</li>
<li>Return Results</li>
</ol>
<p><strong>How the queries are constructed</strong></p>
<ul>
<li>Attribute Values just create a subcube - it creates a new nested subcube for each list, range, or named set it finds</li>
<li>It only supports one measure as written and it just selects that from whatever quagmire of subcubes it's created</li>
</ul>
<p>There are tons of issues with this methodology when we have role playing dimensions because the attribute values will be the same for these dimensions - so only the first one enumerated will get in (there's a way to ignore certain dimensions in the app.config - ignore all but one role playing dimension).  It throws away context and calls it "noise" - now that's dangerous;  if you throw away the context and just look for keywords, it's pretty easy to answer a question that wasn't really asked (oops).</p>
<p>As far as configuring this thing goes - take a look at the app.config and throw in appropriate values for your setup.  You'll have to tweak the *Pattern values to fit things you want to match or exclude in your particular cube.  It's configured for the Adventure Works cube right now, but I didn't work with it much - I just flat out excluded a few dims and hierarchies that would obviously confuse it.</p>
<p>Also note that, as written, it needs an Analysis Services 2008 cube.</p>
<p>Let me know if you want to give it a try and I'll be more than happy to work with you on configuring/modifying it for your setup.  Also post comments on interesting observations you have when asking cubes unqualified questions <img src='http://timlaqua.com/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> </p>
<p><a href='http://timlaqua.com/wp-content/uploads/2009/11/SmartCube.zip'>Download the VS 2008 Project: SmartCube.zip</a></p>
<p>Also note, the project uses an interface called IODoodad for getting questions and responding - this has two public methods, GetInput() and Respond().  The original theory here is that we could make an old-school IRC bot type thing.  We did manage to hook it to Yammer for its IO, respond to private messages and public messages prefixed with @cube, etc.  I didn't really explain the config for YammerIO (there's a custom config section for that that needs a bunch of OAuth stuff).  Again, if you're interested in getting that working, let me know and I'll post instructions.  The project above is set to use ConsoleIO.</p>
<p>I don't plan on doing any more development here, it was just a quick PoC to see what it looked like and get some initial feedback.  If anyone's interested in moving forward with it and making it smarter (the project arguably should have been titled DumbCube at this point), let me know so I can post a link to your work and fork away <img src='http://timlaqua.com/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://timlaqua.com/2009/11/wouldnt-it-be-fun-if-cubes-could-talk/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Charting Analysis Services 2008 Trace Events</title>
		<link>http://timlaqua.com/2009/10/charting-analysis-services-2008-trace-events/</link>
		<comments>http://timlaqua.com/2009/10/charting-analysis-services-2008-trace-events/#comments</comments>
		<pubDate>Sat, 24 Oct 2009 15:05:06 +0000</pubDate>
		<dc:creator>Tim</dc:creator>
				<category><![CDATA[Scripts & Code]]></category>
		<category><![CDATA[analysis services]]></category>
		<category><![CDATA[bi]]></category>
		<category><![CDATA[business intelligence]]></category>
		<category><![CDATA[tsql]]></category>

		<guid isPermaLink="false">http://timlaqua.com/?p=273</guid>
		<description><![CDATA[The other day I was running some Analysis Services traces for some reason or another and ran across Chris Webb's post on (Visualising Analysis Services Trace Information in Reporting Services). After looking over that post, I thought it'd be interesting to visualize various types of processing, queries, etc - basically take a trace and then [...]]]></description>
			<content:encoded><![CDATA[<p>The other day I was running some Analysis Services traces for some reason or another and ran across Chris Webb's post on (<a href="http://cwebbbi.spaces.live.com/Blog/cns!7B84B0F2C239489A!1660.entry">Visualising Analysis Services Trace Information in Reporting Services</a>).  After looking over that post, I thought it'd be interesting to visualize various types of processing, queries, etc - basically take a trace and then graph out what happened and when.  Here's a few samples of what the project yielded:</p>
<ul>
<li style="color: red;">Red: <em>Cube Processing on a 2 Minute schedule</em></li>
<li style="color: green;">Green: <em>Cube Processing on a 1 hour schedule</em></li>
<li style="color: blue;">Blue: <em>Cube Processing on a 1 hour schedule</em></li>
<li style="color: black;">Black: <em>Query Activity</em></li>
</ul>
<p><em>Most of the activity here is from SSRS subscriptions firing around 8AM </em><br />
<a href="http://timlaqua.com/wp-content/uploads/2009/10/8AM-MSRS-Subscription-Processing.png"><img src="http://timlaqua.com/wp-content/uploads/2009/10/8AM-MSRS-Subscription-Processing-300x174.png" alt="8AM MSRS Subscription Processing" title="8AM MSRS Subscription Processing" width="300" height="174" class="alignnone size-medium wp-image-269" /></a><br />
<span id="more-273"></span><br />
<em>This is the report filtered to only show events that had a duration greater than 10,000 MS (10 seconds) - Red cube is a trouble maker</em><br />
<a href="http://timlaqua.com/wp-content/uploads/2009/10/SSAS-Events-Longer-Than-10-Seconds.png"><img src="http://timlaqua.com/wp-content/uploads/2009/10/SSAS-Events-Longer-Than-10-Seconds-300x173.png" alt="SSAS Events Longer Than 10 Seconds" title="SSAS Events Longer Than 10 Seconds" width="300" height="173" class="alignnone size-medium wp-image-270" /></a></p>
<p><em>And this is general SSAS activity over a few hours with the nearly instant events filtered out</em><br />
<a href="http://timlaqua.com/wp-content/uploads/2009/10/General-SSAS-Activity-3-Hours.png"><img src="http://timlaqua.com/wp-content/uploads/2009/10/General-SSAS-Activity-3-Hours-300x173.png" alt="General SSAS Activity (3 Hours)" title="General SSAS Activity (3 Hours)" width="300" height="173" class="alignnone size-medium wp-image-271" /></a></p>
<p>The following query assumes that the trace data ended up in a table:</p>
<ul>
<li>@StartMillisecond: INT; <em>Minimum relative start time (millisecond value, kinda have to guess and check)</em></li>
<li>@EndMillisecond: INT; <em>Maximum relative start time (millisecond value, kinda have to guess and check)</em></li>
<li>@MinDuration: INT; <em>Minimum duration in milliseconds</em></li>
</ul>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;">;WITH <span style="color: #66cc66;">&#91;</span>ProcessEvent<span style="color: #66cc66;">&#93;</span> <span style="color: #993333; font-weight: bold;">AS</span>
<span style="color: #66cc66;">&#40;</span>
<span style="color: #993333; font-weight: bold;">SELECT</span>
	<span style="color: #ff0000;">'Process'</span> <span style="color: #993333; font-weight: bold;">AS</span> <span style="color: #66cc66;">&#91;</span>Event<span style="color: #66cc66;">&#93;</span>
	<span style="color: #66cc66;">,</span>cast<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#91;</span>TextData<span style="color: #66cc66;">&#93;</span> <span style="color: #993333; font-weight: bold;">AS</span> XML<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">.</span>value
		<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'declare namespace B=&quot;http://schemas.microsoft.com/analysisservices/2003/engine&quot;;           
		(//B:DatabaseID)[1]'</span>
		<span style="color: #66cc66;">,</span><span style="color: #ff0000;">'VARCHAR(1024)'</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> <span style="color: #66cc66;">&#91;</span>DatabaseID<span style="color: #66cc66;">&#93;</span>
	<span style="color: #66cc66;">,</span>cast<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#91;</span>TextData<span style="color: #66cc66;">&#93;</span> <span style="color: #993333; font-weight: bold;">AS</span> XML<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">.</span>value
		<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'declare namespace B=&quot;http://schemas.microsoft.com/analysisservices/2003/engine&quot;;           
		(//B:DimensionID)[1]'</span>
		<span style="color: #66cc66;">,</span><span style="color: #ff0000;">'VARCHAR(1024)'</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> <span style="color: #66cc66;">&#91;</span>DimensionID<span style="color: #66cc66;">&#93;</span>
	<span style="color: #66cc66;">,</span>cast<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#91;</span>TextData<span style="color: #66cc66;">&#93;</span> <span style="color: #993333; font-weight: bold;">AS</span> XML<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">.</span>value
		<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'declare namespace B=&quot;http://schemas.microsoft.com/analysisservices/2003/engine&quot;;           
		(//B:MeasureGroupID)[1]'</span>
		<span style="color: #66cc66;">,</span><span style="color: #ff0000;">'VARCHAR(1024)'</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> <span style="color: #66cc66;">&#91;</span>MeasureGroupId<span style="color: #66cc66;">&#93;</span>
	<span style="color: #66cc66;">,</span><span style="color: #66cc66;">&#91;</span>StartTime<span style="color: #66cc66;">&#93;</span>
	<span style="color: #66cc66;">,</span><span style="color: #66cc66;">&#91;</span>Duration<span style="color: #66cc66;">&#93;</span>
<span style="color: #993333; font-weight: bold;">FROM</span> dbo<span style="color: #66cc66;">.</span>zTrace_MSAS_20091023_2 T
<span style="color: #993333; font-weight: bold;">WHERE</span> 
	T<span style="color: #66cc66;">.</span><span style="color: #66cc66;">&#91;</span>EventClass<span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">16</span> 
	<span style="color: #993333; font-weight: bold;">AND</span> T<span style="color: #66cc66;">.</span><span style="color: #66cc66;">&#91;</span>EventSubclass<span style="color: #66cc66;">&#93;</span> <span style="color: #993333; font-weight: bold;">IN</span> <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">3</span><span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">12</span><span style="color: #66cc66;">&#41;</span>
	<span style="color: #993333; font-weight: bold;">AND</span> <span style="color: #66cc66;">&#91;</span>TextData<span style="color: #66cc66;">&#93;</span> <span style="color: #993333; font-weight: bold;">LIKE</span> <span style="color: #ff0000;">'%Process%'</span>
<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span>
<span style="color: #66cc66;">&#91;</span>QueryEvent<span style="color: #66cc66;">&#93;</span> <span style="color: #993333; font-weight: bold;">AS</span>
<span style="color: #66cc66;">&#40;</span>
<span style="color: #993333; font-weight: bold;">SELECT</span>
	<span style="color: #ff0000;">'Query'</span> <span style="color: #993333; font-weight: bold;">AS</span> <span style="color: #66cc66;">&#91;</span>Event<span style="color: #66cc66;">&#93;</span>
	<span style="color: #66cc66;">,</span><span style="color: #66cc66;">&#91;</span>DatabaseName<span style="color: #66cc66;">&#93;</span> <span style="color: #993333; font-weight: bold;">AS</span> <span style="color: #66cc66;">&#91;</span>DatabaseID<span style="color: #66cc66;">&#93;</span>
	<span style="color: #66cc66;">,</span><span style="color: #993333; font-weight: bold;">NULL</span> <span style="color: #993333; font-weight: bold;">AS</span> <span style="color: #66cc66;">&#91;</span>DimensionID<span style="color: #66cc66;">&#93;</span>
	<span style="color: #66cc66;">,</span><span style="color: #993333; font-weight: bold;">NULL</span> <span style="color: #993333; font-weight: bold;">AS</span> <span style="color: #66cc66;">&#91;</span>MeasureGroupId<span style="color: #66cc66;">&#93;</span>
	<span style="color: #66cc66;">,</span><span style="color: #66cc66;">&#91;</span>StartTime<span style="color: #66cc66;">&#93;</span>
	<span style="color: #66cc66;">,</span><span style="color: #66cc66;">&#91;</span>Duration<span style="color: #66cc66;">&#93;</span>
<span style="color: #993333; font-weight: bold;">FROM</span> dbo<span style="color: #66cc66;">.</span>zTrace_MSAS_20091023_2 T
<span style="color: #993333; font-weight: bold;">WHERE</span> T<span style="color: #66cc66;">.</span><span style="color: #66cc66;">&#91;</span>EventClass<span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">10</span>
<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span>
<span style="color: #66cc66;">&#91;</span>Event<span style="color: #66cc66;">&#93;</span> <span style="color: #993333; font-weight: bold;">AS</span>
<span style="color: #66cc66;">&#40;</span>
	<span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #66cc66;">*</span> <span style="color: #993333; font-weight: bold;">FROM</span> <span style="color: #66cc66;">&#91;</span>ProcessEvent<span style="color: #66cc66;">&#93;</span>
	<span style="color: #993333; font-weight: bold;">UNION</span>
	<span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #66cc66;">*</span> <span style="color: #993333; font-weight: bold;">FROM</span> <span style="color: #66cc66;">&#91;</span>QueryEvent<span style="color: #66cc66;">&#93;</span>
<span style="color: #66cc66;">&#41;</span>
<span style="color: #993333; font-weight: bold;">SELECT</span>
	 ROW_NUMBER<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> OVER <span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">ORDER</span> <span style="color: #993333; font-weight: bold;">BY</span> <span style="color: #66cc66;">&#91;</span>StartTime<span style="color: #66cc66;">&#93;</span> <span style="color: #993333; font-weight: bold;">ASC</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> <span style="color: #66cc66;">&#91;</span>EventId<span style="color: #66cc66;">&#93;</span>
	<span style="color: #66cc66;">,</span>DATEADD<span style="color: #66cc66;">&#40;</span>HOUR<span style="color: #66cc66;">,</span> <span style="color: #66cc66;">-</span><span style="color: #cc66cc;">5</span><span style="color: #66cc66;">,</span> <span style="color: #66cc66;">&#91;</span>StartTime<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> <span style="color: #66cc66;">&#91;</span>StartTime<span style="color: #66cc66;">&#93;</span>
	<span style="color: #66cc66;">,</span>DATEDIFF<span style="color: #66cc66;">&#40;</span>MILLISECOND<span style="color: #66cc66;">,</span> <span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">SELECT</span> MIN<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#91;</span>StartTime<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">FROM</span> Event<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> <span style="color: #66cc66;">&#91;</span>StartTime<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#91;</span>RelativeStartTime<span style="color: #66cc66;">&#93;</span>
	<span style="color: #66cc66;">,</span><span style="color: #66cc66;">&#91;</span>Duration<span style="color: #66cc66;">&#93;</span>
	<span style="color: #66cc66;">,</span><span style="color: #66cc66;">&#91;</span>Event<span style="color: #66cc66;">&#93;</span>
	<span style="color: #66cc66;">,</span><span style="color: #66cc66;">&#91;</span>DatabaseID<span style="color: #66cc66;">&#93;</span>
	<span style="color: #66cc66;">,</span>CASE
		WHEN <span style="color: #66cc66;">&#91;</span>DimensionID<span style="color: #66cc66;">&#93;</span> <span style="color: #993333; font-weight: bold;">IS</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span> THEN <span style="color: #ff0000;">'Dimension'</span>
		WHEN <span style="color: #66cc66;">&#91;</span>MeasureGroupID<span style="color: #66cc66;">&#93;</span> <span style="color: #993333; font-weight: bold;">IS</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span> THEN <span style="color: #ff0000;">'Measure Group'</span>
		ELSE <span style="color: #ff0000;">'None'</span>
	 END <span style="color: #993333; font-weight: bold;">AS</span> <span style="color: #66cc66;">&#91;</span>ObjectType<span style="color: #66cc66;">&#93;</span>	<span style="color: #66cc66;">,</span>CASE
		WHEN <span style="color: #66cc66;">&#91;</span>DimensionID<span style="color: #66cc66;">&#93;</span> <span style="color: #993333; font-weight: bold;">IS</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span> THEN <span style="color: #66cc66;">&#91;</span>DimensionID<span style="color: #66cc66;">&#93;</span>
		WHEN <span style="color: #66cc66;">&#91;</span>MeasureGroupID<span style="color: #66cc66;">&#93;</span> <span style="color: #993333; font-weight: bold;">IS</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span> THEN <span style="color: #66cc66;">&#91;</span>MeasureGroupID<span style="color: #66cc66;">&#93;</span>
		ELSE <span style="color: #ff0000;">'None'</span>
	 END <span style="color: #993333; font-weight: bold;">AS</span> <span style="color: #66cc66;">&#91;</span>ObjectId<span style="color: #66cc66;">&#93;</span>	
<span style="color: #993333; font-weight: bold;">FROM</span> <span style="color: #66cc66;">&#91;</span>Event<span style="color: #66cc66;">&#93;</span>
<span style="color: #993333; font-weight: bold;">WHERE</span>
	DATEDIFF<span style="color: #66cc66;">&#40;</span>MILLISECOND<span style="color: #66cc66;">,</span> <span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">SELECT</span> MIN<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#91;</span>StartTime<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">FROM</span> Event<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> <span style="color: #66cc66;">&#91;</span>StartTime<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&gt;=</span> @StartMillisecond
	<span style="color: #993333; font-weight: bold;">AND</span> DATEDIFF<span style="color: #66cc66;">&#40;</span>MILLISECOND<span style="color: #66cc66;">,</span> <span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">SELECT</span> MIN<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#91;</span>StartTime<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">FROM</span> Event<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> <span style="color: #66cc66;">&#91;</span>StartTime<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&lt;</span> @EndMillisecond
	<span style="color: #993333; font-weight: bold;">AND</span> Duration <span style="color: #66cc66;">&gt;=</span> @MinDuration</pre></div></div>

<p>A few notes on the report:</p>
<ul>
<li>[EventId] is the Series Group</li>
<li>This is a Range chart type, the Bottom value was [RelativeStartTime] and the Top value was [RelativeStartTime] + [Duration]</li>
<li>Coloring the series and markers uses the following expression

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008000;">=</span>Iif<span style="color: #000000;">&#40;</span>Fields<span style="color: #008000;">!</span><span style="color: #0600FF;">Event</span>.<span style="color: #0000FF;">Value</span> <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;Process&quot;</span>
	,Iif<span style="color: #000000;">&#40;</span>Fields<span style="color: #008000;">!</span>DatabaseID.<span style="color: #0000FF;">Value</span> <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;SSASDB&quot;</span>
		,<span style="color: #666666;">&quot;Red&quot;</span>
		,Iif<span style="color: #000000;">&#40;</span>Fields<span style="color: #008000;">!</span>DatabaseID.<span style="color: #0000FF;">Value</span> <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;OtherSSASDB&quot;</span>
			,<span style="color: #666666;">&quot;Green&quot;</span>
			,Iif<span style="color: #000000;">&#40;</span>Fields<span style="color: #008000;">!</span>DatabaseID.<span style="color: #0000FF;">Value</span> <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;YetAnotherSSASDB&quot;</span>
				,<span style="color: #666666;">&quot;Blue&quot;</span>
				,<span style="color: #666666;">&quot;Pink&quot;</span>
			<span style="color: #000000;">&#41;</span>
		<span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#41;</span>
	,<span style="color: #666666;">&quot;Black&quot;</span>
<span style="color: #000000;">&#41;</span></pre></div></div>

</li>
</ul>
<p>And that's about it - if you have any more questions about it just let me know.  Just seemed like an interesting way to look at trace data <img src='http://timlaqua.com/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' />   It also wouldn't be that difficult to generate events for other things, like your ETL processing and such so you could see that on top of everything else.</p>
]]></content:encoded>
			<wfw:commentRss>http://timlaqua.com/2009/10/charting-analysis-services-2008-trace-events/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Reporting Services (SSRS/MSRS) 2008 Error: Set used with the complement operator must have all members from the same level</title>
		<link>http://timlaqua.com/2009/10/reporting-services-ssrsmsrs-2008-error-set-used-with-the-complement-operator-must-have-all-members-from-the-same-level/</link>
		<comments>http://timlaqua.com/2009/10/reporting-services-ssrsmsrs-2008-error-set-used-with-the-complement-operator-must-have-all-members-from-the-same-level/#comments</comments>
		<pubDate>Thu, 01 Oct 2009 19:38:06 +0000</pubDate>
		<dc:creator>Tim</dc:creator>
				<category><![CDATA[Scripts & Code]]></category>
		<category><![CDATA[bi]]></category>
		<category><![CDATA[business intelligence]]></category>
		<category><![CDATA[mdx]]></category>
		<category><![CDATA[msrs]]></category>
		<category><![CDATA[reporting services]]></category>
		<category><![CDATA[ssrs]]></category>

		<guid isPermaLink="false">http://timlaqua.com/?p=244</guid>
		<description><![CDATA[When you use the Not In operator in a SSRS 2008 MDX query filter to exclude a named set, it uses a the complement operator in the constructed MDX. This is fine as long as "all members [are] from the same level." Since you got this error, they are not You can get around this [...]]]></description>
			<content:encoded><![CDATA[<p>When you use the <strong>Not In</strong> operator in a SSRS 2008 MDX query filter to exclude a named set, it uses a the complement operator in the constructed MDX.  This is fine as long as "all members [are] from the same level."  Since you got this error, they are not <img src='http://timlaqua.com/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' />   You can get around this by using the Except() MDX function instead of letting SSRS use the Complement operator</p>
<p>In the ReportServerService log, you'll see something like this:<br />
<em>Microsoft.AnalysisServices.AdomdClient.AdomdErrorResponseException: Query (..., ...) Set used with the complement operator must have all members from the same level.</em></p>
<p><u>Original filter</u><br />
Dimension: <strong>Time</strong><br />
Hierarchy: <strong>Calendar Date</strong><br />
Operator: <strong>Not In</strong><br />
Filter Expression: <strong>[Today]</strong></p>
<p><u>New filter</u><br />
Dimension: <strong>Time</strong><br />
Hierarchy: <strong>Calendar Date</strong><br />
Operator: <strong>MDX</strong><br />
Filter Expression: <strong>Except([Time].[Calendar Date].[Calendar Date].MEMBERS, [Today])</strong></p>
]]></content:encoded>
			<wfw:commentRss>http://timlaqua.com/2009/10/reporting-services-ssrsmsrs-2008-error-set-used-with-the-complement-operator-must-have-all-members-from-the-same-level/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Maintaining a Type 1 Slowly Changing Dimension (SCD) using T-SQL</title>
		<link>http://timlaqua.com/2009/05/maintaining-a-type-1-slowly-changing-dimension-scd-using-t-sql/</link>
		<comments>http://timlaqua.com/2009/05/maintaining-a-type-1-slowly-changing-dimension-scd-using-t-sql/#comments</comments>
		<pubDate>Sat, 23 May 2009 16:37:35 +0000</pubDate>
		<dc:creator>Tim</dc:creator>
				<category><![CDATA[Scripts & Code]]></category>
		<category><![CDATA[bi]]></category>
		<category><![CDATA[business intelligence]]></category>
		<category><![CDATA[data warehouse]]></category>
		<category><![CDATA[scd]]></category>
		<category><![CDATA[slowly changing dimension]]></category>
		<category><![CDATA[sql]]></category>
		<category><![CDATA[ssis]]></category>
		<category><![CDATA[t-sql]]></category>

		<guid isPermaLink="false">http://timlaqua.com/?p=224</guid>
		<description><![CDATA[UPDATE 2012-03-16: Please also take a look at Slowly Changing Dimensions with MD5 Hashes in SSIS which we have determined to be fastest, most efficient approach to maintaining Type 1 dimensions. A few days ago, one of our SSIS packages that maintained a Type 1 Slowly Changing Dimension (SCD) of about 1 million rows crept [...]]]></description>
			<content:encoded><![CDATA[<p><strong>UPDATE 2012-03-16: Please also take a look at <a href="http://timlaqua.com/2012/02/slowly-changing-dimensions-with-md5-hashes-in-ssis/" title="Slowly Changing Dimensions with MD5 Hashes in SSIS">Slowly Changing Dimensions with MD5 Hashes in SSIS</a> which we have determined to be fastest, most efficient approach to maintaining Type 1 dimensions.</strong></p>
<p>A few days ago, one of our SSIS packages that maintained a Type 1 Slowly Changing Dimension (SCD) of about 1 million rows crept up to 15 minutes of runtime.  Now this doesn't sound too bad, but this is part of our hourly batches, so 15 minutes is 25% of our entire processing window.  The package was using the Slowly Changing Dimension Wizard transformation - we were doing the standard OLEDB Source (which basically represented how the SCD "should" look) and then sending it to the SCD transform and letting it figure out what needed to be inserted and updated.  One option was to switch to lookups instead of the SCD wizard to speed things up, maybe even some fancy checksum voodoo for the updates (see <a href="http://blog.stevienova.com/2008/11/22/ssis-slowly-changing-dimensions-with-checksum/">http://blog.stevienova.com/2008/11/22/ssis-slowly-changing-dimensions-with-checksum/</a> for an example).  Then after thinking about it a little more - why are we sending a million rows down the pipeline every hour?  We know only a small percentage of these are new - and another small percentage needs to be updated.  Well, we can just write a quick SQL query to get us just those sets and the package would be much more efficient!  </p>
<p>Wait a tick - why would we give the rows to SSIS if all it is going to do insert one set and update the other?  Let's just do it all in T-SQL:<span id="more-224"></span></p>
<p><em>The following tables and dim are fictional - I just need to make up a star schema DIM to illustrate the approach</em></p>

<div class="wp_syntax"><div class="code"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">CREATE</span> <span style="color: #0000FF;">TABLE</span> #Temp_SCDInserts <span style="color: #808080;">&#40;</span><span style="color: #808080;">&#91;</span>PartId<span style="color: #808080;">&#93;</span> <span style="color: #0000FF;">INT</span><span style="color: #808080;">&#41;</span>
&nbsp;
<span style="color: #008080;">-- INSERTS for new Parts</span>
<span style="color: #0000FF;">INSERT</span> <span style="color: #0000FF;">INTO</span> #Temp_SCDInserts
<span style="color: #0000FF;">SELECT</span>   
	<span style="color: #808080;">&#91;</span>PartId<span style="color: #808080;">&#93;</span>
<span style="color: #0000FF;">FROM</span> 
	Part a <span style="color: #0000FF;">WITH</span><span style="color: #808080;">&#40;</span>NOLOCK<span style="color: #808080;">&#41;</span>
	<span style="color: #0000FF;">INNER</span> <span style="color: #808080;">JOIN</span> PartType b <span style="color: #0000FF;">WITH</span><span style="color: #808080;">&#40;</span>NOLOCK<span style="color: #808080;">&#41;</span> 
		<span style="color: #0000FF;">ON</span> a.<span style="color: #202020;">PartTypeId</span> <span style="color: #808080;">=</span> b.<span style="color: #202020;">PartTypeId</span>
	<span style="color: #0000FF;">INNER</span> <span style="color: #808080;">JOIN</span> PartSupplier c <span style="color: #0000FF;">WITH</span><span style="color: #808080;">&#40;</span>NOLOCK<span style="color: #808080;">&#41;</span> 
		<span style="color: #0000FF;">ON</span> a.<span style="color: #202020;">PartSupplierId</span> <span style="color: #808080;">=</span> c.<span style="color: #202020;">PartSupplierId</span>
	<span style="color: #0000FF;">INNER</span> <span style="color: #808080;">JOIN</span> PartSupplierCategory d <span style="color: #0000FF;">WITH</span><span style="color: #808080;">&#40;</span>NOLOCK<span style="color: #808080;">&#41;</span> 
		<span style="color: #0000FF;">ON</span> c.<span style="color: #202020;">PartSupplierCategoryId</span> <span style="color: #808080;">=</span> d.<span style="color: #202020;">PartSupplierCategoryId</span>
<span style="color: #0000FF;">EXCEPT</span>
<span style="color: #0000FF;">SELECT</span>
	<span style="color: #808080;">&#91;</span>PartId<span style="color: #808080;">&#93;</span>	
<span style="color: #0000FF;">FROM</span>
	DW.<span style="color: #202020;">dbo</span>.<span style="color: #202020;">Dim_Part</span> <span style="color: #0000FF;">WITH</span><span style="color: #808080;">&#40;</span>NOLOCK<span style="color: #808080;">&#41;</span>
&nbsp;
<span style="color: #008080;">-- UPDATES</span>
<span style="color: #0000FF;">SELECT</span> 
	 dim.<span style="color: #808080;">&#91;</span>PartKeyId<span style="color: #808080;">&#93;</span>
	,a.<span style="color: #808080;">&#91;</span>PartName<span style="color: #808080;">&#93;</span>
	,b.<span style="color: #808080;">&#91;</span>PartTypeId<span style="color: #808080;">&#93;</span>
	,b.<span style="color: #808080;">&#91;</span>PartTypeName<span style="color: #808080;">&#93;</span>
	,c.<span style="color: #808080;">&#91;</span>PartSupplierId<span style="color: #808080;">&#93;</span>
	,c.<span style="color: #808080;">&#91;</span>PartSupplierName<span style="color: #808080;">&#93;</span>
	,d.<span style="color: #808080;">&#91;</span>PartSupplierCategoryId<span style="color: #808080;">&#93;</span>
	,d.<span style="color: #808080;">&#91;</span>PartSupplierCategoryName<span style="color: #808080;">&#93;</span>
<span style="color: #0000FF;">INTO</span> #Temp_SCDUpdates
<span style="color: #0000FF;">FROM</span> DW.<span style="color: #202020;">dbo</span>.<span style="color: #202020;">Dim_Part</span> dim <span style="color: #0000FF;">WITH</span><span style="color: #808080;">&#40;</span>NOLOCK<span style="color: #808080;">&#41;</span>
	<span style="color: #0000FF;">INNER</span> <span style="color: #808080;">JOIN</span> Part a <span style="color: #0000FF;">WITH</span><span style="color: #808080;">&#40;</span>NOLOCK<span style="color: #808080;">&#41;</span>
		<span style="color: #0000FF;">ON</span> dim.<span style="color: #202020;">PartId</span> <span style="color: #808080;">=</span> a.<span style="color: #202020;">PartId</span> <span style="color: #008080;">-- Business Key</span>
	<span style="color: #0000FF;">INNER</span> <span style="color: #808080;">JOIN</span> PartType b <span style="color: #0000FF;">WITH</span><span style="color: #808080;">&#40;</span>NOLOCK<span style="color: #808080;">&#41;</span> 
		<span style="color: #0000FF;">ON</span> a.<span style="color: #202020;">PartTypeId</span> <span style="color: #808080;">=</span> b.<span style="color: #202020;">PartTypeId</span>
	<span style="color: #0000FF;">INNER</span> <span style="color: #808080;">JOIN</span> PartSupplier c <span style="color: #0000FF;">WITH</span><span style="color: #808080;">&#40;</span>NOLOCK<span style="color: #808080;">&#41;</span> 
		<span style="color: #0000FF;">ON</span> a.<span style="color: #202020;">PartSupplierId</span> <span style="color: #808080;">=</span> c.<span style="color: #202020;">PartSupplierId</span>
	<span style="color: #0000FF;">INNER</span> <span style="color: #808080;">JOIN</span> PartSupplierCategory d <span style="color: #0000FF;">WITH</span><span style="color: #808080;">&#40;</span>NOLOCK<span style="color: #808080;">&#41;</span> 
		<span style="color: #0000FF;">ON</span> c.<span style="color: #202020;">PartSupplierCategoryId</span> <span style="color: #808080;">=</span> d.<span style="color: #202020;">PartSupplierCategoryId</span>
<span style="color: #0000FF;">WHERE</span>
	dim.<span style="color: #202020;">PartName</span> <span style="color: #808080;">&lt;&gt;</span> a.<span style="color: #202020;">PartName</span>
	<span style="color: #808080;">OR</span> dim.<span style="color: #202020;">PartTypeId</span> <span style="color: #808080;">&lt;&gt;</span> b.<span style="color: #202020;">PartTypeId</span>
	<span style="color: #808080;">OR</span> dim.<span style="color: #202020;">PartTypeName</span> <span style="color: #808080;">&lt;&gt;</span> b.<span style="color: #202020;">PartTypeName</span>
	<span style="color: #808080;">OR</span> dim.<span style="color: #202020;">PartSupplierId</span> <span style="color: #808080;">&lt;&gt;</span> c.<span style="color: #202020;">PartSupplierId</span>
	<span style="color: #808080;">OR</span> dim.<span style="color: #202020;">PartSupplierName</span> <span style="color: #808080;">&lt;&gt;</span> c.<span style="color: #202020;">PartSupplierName</span>
	<span style="color: #808080;">OR</span> dim.<span style="color: #202020;">PartSupplierCategoryId</span> <span style="color: #808080;">&lt;&gt;</span> d.<span style="color: #202020;">PartSupplierCategoryId</span>
	<span style="color: #808080;">OR</span> dim.<span style="color: #202020;">PartSupplierCategoryName</span>	<span style="color: #808080;">&lt;&gt;</span> d.<span style="color: #202020;">PartSupplierCategoryName</span>
&nbsp;
<span style="color: #008080;">-- INSERT new records</span>
<span style="color: #0000FF;">INSERT</span> <span style="color: #0000FF;">INTO</span> <span style="color: #808080;">&#91;</span>DW<span style="color: #808080;">&#93;</span>.<span style="color: #808080;">&#91;</span>dbo<span style="color: #808080;">&#93;</span>.<span style="color: #808080;">&#91;</span>Dim_Part<span style="color: #808080;">&#93;</span>
	<span style="color: #808080;">&#40;</span><span style="color: #808080;">&#91;</span>PartId<span style="color: #808080;">&#93;</span>
	,<span style="color: #808080;">&#91;</span>PartName<span style="color: #808080;">&#93;</span>
	,<span style="color: #808080;">&#91;</span>PartTypeId<span style="color: #808080;">&#93;</span>
	,<span style="color: #808080;">&#91;</span>PartTypeName<span style="color: #808080;">&#93;</span>
	,<span style="color: #808080;">&#91;</span>PartSupplierId<span style="color: #808080;">&#93;</span>
	,<span style="color: #808080;">&#91;</span>PartSupplierName<span style="color: #808080;">&#93;</span>
	,<span style="color: #808080;">&#91;</span>PartSupplierCategoryId<span style="color: #808080;">&#93;</span>
	,<span style="color: #808080;">&#91;</span>PartSupplierCategoryName<span style="color: #808080;">&#93;</span><span style="color: #808080;">&#41;</span>
<span style="color: #0000FF;">SELECT</span> 
	 a.<span style="color: #808080;">&#91;</span>PartId<span style="color: #808080;">&#93;</span>
	,a.<span style="color: #808080;">&#91;</span>PartName<span style="color: #808080;">&#93;</span>
	,b.<span style="color: #808080;">&#91;</span>PartTypeId<span style="color: #808080;">&#93;</span>
	,b.<span style="color: #808080;">&#91;</span>PartTypeName<span style="color: #808080;">&#93;</span>
	,c.<span style="color: #808080;">&#91;</span>PartSupplierId<span style="color: #808080;">&#93;</span>
	,c.<span style="color: #808080;">&#91;</span>PartSupplierName<span style="color: #808080;">&#93;</span>
	,d.<span style="color: #808080;">&#91;</span>PartSupplierCategoryId<span style="color: #808080;">&#93;</span>
	,d.<span style="color: #808080;">&#91;</span>PartSupplierCategoryName<span style="color: #808080;">&#93;</span>
<span style="color: #0000FF;">FROM</span> #Temp_SCDInserts i
	<span style="color: #0000FF;">INNER</span> <span style="color: #808080;">JOIN</span> Part a <span style="color: #0000FF;">WITH</span><span style="color: #808080;">&#40;</span>NOLOCK<span style="color: #808080;">&#41;</span>
		<span style="color: #0000FF;">ON</span> i.<span style="color: #202020;">PartId</span> <span style="color: #808080;">=</span> a.<span style="color: #202020;">PartId</span> <span style="color: #008080;">-- Business Key</span>
	<span style="color: #0000FF;">INNER</span> <span style="color: #808080;">JOIN</span> PartType b <span style="color: #0000FF;">WITH</span><span style="color: #808080;">&#40;</span>NOLOCK<span style="color: #808080;">&#41;</span> 
		<span style="color: #0000FF;">ON</span> a.<span style="color: #202020;">PartTypeId</span> <span style="color: #808080;">=</span> b.<span style="color: #202020;">PartTypeId</span>
	<span style="color: #0000FF;">INNER</span> <span style="color: #808080;">JOIN</span> PartSupplier c <span style="color: #0000FF;">WITH</span><span style="color: #808080;">&#40;</span>NOLOCK<span style="color: #808080;">&#41;</span> 
		<span style="color: #0000FF;">ON</span> a.<span style="color: #202020;">PartSupplierId</span> <span style="color: #808080;">=</span> c.<span style="color: #202020;">PartSupplierId</span>
	<span style="color: #0000FF;">INNER</span> <span style="color: #808080;">JOIN</span> PartSupplierCategory d <span style="color: #0000FF;">WITH</span><span style="color: #808080;">&#40;</span>NOLOCK<span style="color: #808080;">&#41;</span> 
		<span style="color: #0000FF;">ON</span> c.<span style="color: #202020;">PartSupplierCategoryId</span> <span style="color: #808080;">=</span> d.<span style="color: #202020;">PartSupplierCategoryId</span>
&nbsp;
<span style="color: #008080;">-- UPDATE existing records</span>
<span style="color: #0000FF;">UPDATE</span> <span style="color: #808080;">&#91;</span>DW<span style="color: #808080;">&#93;</span>.<span style="color: #808080;">&#91;</span>dbo<span style="color: #808080;">&#93;</span>.<span style="color: #808080;">&#91;</span>Dim_Part<span style="color: #808080;">&#93;</span>
<span style="color: #0000FF;">SET</span>
	 <span style="color: #808080;">&#91;</span>PartName<span style="color: #808080;">&#93;</span> <span style="color: #808080;">=</span> b.<span style="color: #808080;">&#91;</span>PartName<span style="color: #808080;">&#93;</span>
	,<span style="color: #808080;">&#91;</span>PartTypeId<span style="color: #808080;">&#93;</span> <span style="color: #808080;">=</span> b.<span style="color: #808080;">&#91;</span>PartTypeId<span style="color: #808080;">&#93;</span>
	,<span style="color: #808080;">&#91;</span>PartTypeName<span style="color: #808080;">&#93;</span> <span style="color: #808080;">=</span> b.<span style="color: #808080;">&#91;</span>PartTypeName<span style="color: #808080;">&#93;</span>
	,<span style="color: #808080;">&#91;</span>PartSupplierId<span style="color: #808080;">&#93;</span> <span style="color: #808080;">=</span> b.<span style="color: #808080;">&#91;</span>PartSupplierId<span style="color: #808080;">&#93;</span>
	,<span style="color: #808080;">&#91;</span>PartSupplierName<span style="color: #808080;">&#93;</span> <span style="color: #808080;">=</span> b.<span style="color: #808080;">&#91;</span>PartSupplierName<span style="color: #808080;">&#93;</span>
	,<span style="color: #808080;">&#91;</span>PartSupplierCategoryId<span style="color: #808080;">&#93;</span> <span style="color: #808080;">=</span> b.<span style="color: #808080;">&#91;</span>PartSupplierCategoryId<span style="color: #808080;">&#93;</span>
	,<span style="color: #808080;">&#91;</span>PartSupplierCategoryName<span style="color: #808080;">&#93;</span> <span style="color: #808080;">=</span> b.<span style="color: #808080;">&#91;</span>PartSupplierCategoryName<span style="color: #808080;">&#93;</span>
<span style="color: #0000FF;">FROM</span> 
	<span style="color: #808080;">&#91;</span>DW<span style="color: #808080;">&#93;</span>.<span style="color: #808080;">&#91;</span>dbo<span style="color: #808080;">&#93;</span>.<span style="color: #808080;">&#91;</span>Dim_Part<span style="color: #808080;">&#93;</span> <span style="color: #0000FF;">AS</span> a
	<span style="color: #0000FF;">INNER</span> <span style="color: #808080;">JOIN</span> #Temp_SCDUpdates <span style="color: #0000FF;">AS</span> b 
		<span style="color: #0000FF;">ON</span> a.<span style="color: #202020;">PartKeyId</span> <span style="color: #808080;">=</span> b.<span style="color: #202020;">PartKeyId</span>  <span style="color: #008080;">-- Join on Business Keys or Surrogate Key</span>
&nbsp;
&nbsp;
<span style="color: #0000FF;">DROP</span> <span style="color: #0000FF;">TABLE</span> #Temp_SCDInserts
<span style="color: #0000FF;">DROP</span> <span style="color: #0000FF;">TABLE</span> #Temp_SCDUpdates</pre></div></div>

<p>The results?  Our 15 minute SSIS package has been replaced with a few lines of T-SQL and it now runs in less than 90 seconds.  Nice.</p>
]]></content:encoded>
			<wfw:commentRss>http://timlaqua.com/2009/05/maintaining-a-type-1-slowly-changing-dimension-scd-using-t-sql/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

