<?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; ssis</title>
	<atom:link href="http://timlaqua.com/tag/ssis/feed/" rel="self" type="application/rss+xml" />
	<link>http://timlaqua.com</link>
	<description>Thoughts and Code from Tim Laqua</description>
	<lastBuildDate>Fri, 03 Feb 2012 23:12:27 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>SSIS, BIDS and 64bit System DSNs: Who&#8217;s on first?</title>
		<link>http://timlaqua.com/2011/11/ssis-bids-and-64bit-system-dsns-whos-on-first/</link>
		<comments>http://timlaqua.com/2011/11/ssis-bids-and-64bit-system-dsns-whos-on-first/#comments</comments>
		<pubDate>Fri, 18 Nov 2011 20:34:37 +0000</pubDate>
		<dc:creator>Tim</dc:creator>
				<category><![CDATA[Scripts & Code]]></category>
		<category><![CDATA[bi]]></category>
		<category><![CDATA[ssis]]></category>

		<guid isPermaLink="false">http://timlaqua.com/?p=828</guid>
		<description><![CDATA[After an hour or so of changing settings, package configurations, protection levels, passwords, connection strings, etc - you might just still have yourself one of those dreaded red boxes (and a headache). And you thought inserting data using ODBC was easy... You might even have yourself some logging that indicates an error like this: [AS400 [...]]]></description>
			<content:encoded><![CDATA[<p>After an hour or so of changing settings, package configurations, protection levels, passwords, connection strings, etc - you  might just still have yourself one of those dreaded red boxes (and a headache).  And you thought inserting data using ODBC was easy...</p>
<p><a href="http://timlaqua.com/wp-content/uploads/2011/11/Execute-Package-Failed-To-Acquire-Connection.png"><img src="http://timlaqua.com/wp-content/uploads/2011/11/Execute-Package-Failed-To-Acquire-Connection.png" alt="" title="Execute Package - Failed To Acquire Connection" width="774" height="551" class="alignnone size-full wp-image-833" /></a></p>
<p>You might even have yourself some logging that indicates an error like this:<br />
<strong>[AS400 [19]] Error: ADO NET Destination has failed to acquire the connection {43E6AE37-24E8-46F6-8AB0-689DB6531167}. The connection may have been corrupted.</strong></p>
<p>Assuming you are using a System DSN for your connection (this article is based on an ODBC DSN using the iSeries Access ODBC Driver to write to DB2, but it applies to any ODBC driver with 32bit and 64bit versions where a System DSN is used for the connection).</p>
<p>Before going on, you should understand and have checked the <strong>PackageProtectionLevel</strong> property in your package.  To get any connection that requires a password to run in BIDS, you can't use DontSaveSensitive (unless you're using package configurations to enter the password or an explicit connection string).  This is by far the most common reason connections fail to validate - but there's many resources out there already on that situation, so I won't go in to it.</p>
<p>Now that we know it SHOULD work - why doesn't it?  In short, because you don't have a 64-bit version of the System DSN you selected.  What sense does that make?  Let me show you.<br />
<span id="more-828"></span><br />
<a href="http://timlaqua.com/wp-content/uploads/2011/11/ODBC-Administration-Programs.png"><img src="http://timlaqua.com/wp-content/uploads/2011/11/ODBC-Administration-Programs.png" alt="" title="ODBC Administration Programs" width="202" height="71" class="alignnone size-full wp-image-835" /></a></p>
<p>I'm sure you've all seen this before - which one do we select?  I'm apparently pretty random with it as when I started this investigation, I found various different System DSNs in both places (and they didn't match).  Maybe you just use the Control Panel to get to your ODBC Data Sources:</p>
<p><a href="http://timlaqua.com/wp-content/uploads/2011/11/Data-sources-ODBC-Control-Panel.png"><img src="http://timlaqua.com/wp-content/uploads/2011/11/Data-sources-ODBC-Control-Panel.png" alt="" title="Data sources (ODBC) Control Panel" width="234" height="155" class="alignnone size-full wp-image-831" /></a></p>
<p>Interesting - we know there's two places to manage ODBC connections...  Which one is this?  It's the 64bit one.  Which means if this is the only way you enter System DSNs, all you ever enter are 64bit System DSNs.</p>
<p>Now, on to how this happened in the first place.  First, I'll create both a 64bit DSN and a 32bit DSN:<br />
<a href="http://timlaqua.com/wp-content/uploads/2011/11/ODBC-64bit.png"><img src="http://timlaqua.com/wp-content/uploads/2011/11/ODBC-64bit.png" alt="" title="ODBC 64bit" width="471" height="390" class="alignnone size-full wp-image-841" /></a><br />
<a href="http://timlaqua.com/wp-content/uploads/2011/11/ODBC-32bit.png"><img src="http://timlaqua.com/wp-content/uploads/2011/11/ODBC-32bit.png" alt="" title="ODBC 32bit" width="471" height="390" class="alignnone size-full wp-image-842" /></a></p>
<p>Then in SSIS, we create an ODBC connection, the dropdown lists our DSNs for us like so:</p>
<p><a href="http://timlaqua.com/wp-content/uploads/2011/11/ADO-Connection-Manager-ODBC-32bit-DSN.png"><img src="http://timlaqua.com/wp-content/uploads/2011/11/ADO-Connection-Manager-ODBC-32bit-DSN.png" alt="" title="ADO Connection Manager - ODBC - 32bit DSN" width="608" height="623" class="alignnone size-full wp-image-830" /></a></p>
<p>Notice that these are a mismash of both your User DSNs and 32bit System DSNs (and probably File DSNs... but who uses those anyway).  No 64bit DSNs are listed.  As a side note, this also presents itself when you create a "System DSN" (not knowing you're creating a 64bit specific DSN), then you go in to BIDS and can't find your shiny new DSN in the list.  To get your DSN to show, you probably go back in and create a User DSN, and it will then show up.  Until you try to run it on the server under a different username - which can be even more confusing.</p>
<p>To test if you are encountering this 32/64bit DSN issue, you can change your SSIS Solution properties to use the 32bit runtime:</p>
<p><a href="http://timlaqua.com/wp-content/uploads/2011/11/Debugging-Configuration.png"><img src="http://timlaqua.com/wp-content/uploads/2011/11/Debugging-Configuration.png" alt="" title="Debugging Configuration" width="701" height="425" class="alignnone size-full wp-image-832" /></a></p>
<p>Now run your package again and it should look a little more like this:</p>
<p><a href="http://timlaqua.com/wp-content/uploads/2011/11/Execute-Package-Success-32bit.png"><img src="http://timlaqua.com/wp-content/uploads/2011/11/Execute-Package-Success-32bit.png" alt="" title="Execute Package - Success - 32bit" width="774" height="551" class="alignnone size-full wp-image-834" /></a></p>
<p>You can also take your compiled package and run it with the 32bit version of dtexec and the 64bit version.  Here's what that should look like if you're having this issue:</p>
<p><em>Use the dtexec located under <strong>Program Files\Microsoft SQL Server\100\DTS\Binn</strong></em></p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">C:\&gt;&quot;C:\Program Files\Microsoft SQL Server\100\DTS\Binn\dtexec&quot; /FILE &quot;C:\Users\
tlaqua\Documents\Visual Studio 2008\Projects\AS400 ODBC Test\AS400 ODBC Test\bin
\ExportToAS400.dtsx&quot; /CHECKPOINTING OFF  /REPORTING EWCDI
Microsoft (R) SQL Server Execute Package Utility
Version 10.50.2500.0 for 64-bit
Copyright (C) Microsoft Corporation 2010. All rights reserved.
&nbsp;
Started:  12:29:04 PM
Info: 2011-11-18 12:29:04.24
   Code: 0x4004300A
   Source: Export to AS400 SSIS.Pipeline
   Description: Validation phase is beginning.
End Info
Error: 2011-11-18 12:29:05.21
   Code: 0xC0208452
   Source: Export to AS400 AS400 [19]
   Description: ADO NET Destination has failed to acquire the connection {43E6AE
37-24E8-46F6-8AB0-689DB6531167}. The connection may have been corrupted.
End Error
Error: 2011-11-18 12:29:05.21
   Code: 0xC0047017
   Source: Export to AS400 SSIS.Pipeline
   Description: component &quot;AS400&quot; (19) failed validation and returned error code
 0xC0208452.
End Error
Error: 2011-11-18 12:29:05.21
   Code: 0xC004700C
   Source: Export to AS400 SSIS.Pipeline
   Description: One or more component failed validation.
End Error
Error: 2011-11-18 12:29:05.21
   Code: 0xC0024107
   Source: Export to AS400
   Description: There were errors during task validation.
End Error
DTExec: The package execution returned DTSER_FAILURE (1).
Started:  12:29:04 PM
Finished: 12:29:05 PM
Elapsed:  1.029 seconds</pre></div></div>

<p><em>Use the dtexec located under <strong>Program Files<span style="background-color: yellow;"> (x86)</span>\Microsoft SQL Server\100\DTS\Binn</strong></em></p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">C:\&gt;&quot;C:\Program Files (x86)\Microsoft SQL Server\100\DTS\Binn\dtexec&quot; /FILE &quot;C:\
Users\tlaqua\Documents\Visual Studio 2008\Projects\AS400 ODBC Test\AS400 ODBC Te
st\bin\ExportToAS400.dtsx&quot; /CHECKPOINTING OFF  /REPORTING EWCDI
Microsoft (R) SQL Server Execute Package Utility
Version 10.50.2500.0 for 32-bit
Copyright (C) Microsoft Corporation 2010. All rights reserved.
&nbsp;
Started:  12:29:49 PM
Info: 2011-11-18 12:29:49.96
   Code: 0x4004300A
   Source: Export to AS400 SSIS.Pipeline
   Description: Validation phase is beginning.
End Info
Info: 2011-11-18 12:29:51.14
   Code: 0x4004300A
   Source: Export to AS400 SSIS.Pipeline
   Description: Validation phase is beginning.
End Info
Info: 2011-11-18 12:29:51.41
   Code: 0x40043006
   Source: Export to AS400 SSIS.Pipeline
   Description: Prepare for Execute phase is beginning.
End Info
Info: 2011-11-18 12:29:51.58
   Code: 0x40043007
   Source: Export to AS400 SSIS.Pipeline
   Description: Pre-Execute phase is beginning.
End Info
Info: 2011-11-18 12:29:51.94
   Code: 0x4004300C
   Source: Export to AS400 SSIS.Pipeline
   Description: Execute phase is beginning.
End Info
DataFlow: 2011-11-18 12:29:51.94
   Source: Export to AS400
   Component &quot;AS400&quot; (19) will receive 2 rows on input &quot;ADO NET Destination Inpu
t&quot; (22)
End DataFlow
DataFlow: 2011-11-18 12:29:52.11
   Source: Export to AS400
   Component &quot;AS400&quot; (19) was given end of rowset on input &quot;ADO NET Destination
Input&quot; (22)
End DataFlow
DataFlow: 2011-11-18 12:29:52.11
   Source: Export to AS400
   Component &quot;AS400&quot; (19) will receive 0 rows on input &quot;ADO NET Destination Inpu
t&quot; (22)
End DataFlow
Info: 2011-11-18 12:29:52.11
   Code: 0x40043008
   Source: Export to AS400 SSIS.Pipeline
   Description: Post Execute phase is beginning.
End Info
Info: 2011-11-18 12:29:52.14
   Code: 0x4004300B
   Source: Export to AS400 SSIS.Pipeline
   Description: &quot;component &quot;AS400&quot; (19)&quot; wrote 2 rows.
End Info
Info: 2011-11-18 12:29:52.14
   Code: 0x40043009
   Source: Export to AS400 SSIS.Pipeline
   Description: Cleanup phase is beginning.
End Info
DTExec: The package execution returned DTSER_SUCCESS (0).
Started:  12:29:49 PM
Finished: 12:29:52 PM
Elapsed:  2.574 seconds</pre></div></div>

<p>So how do we avoid/fix this?</p>
<ol>
<li>Ensure you setup the EXACT same System DSNs in both 64bit and 32bit on your development machine.
<li>On your server(s), you most likely only need to setup the 64bit DSNs as all they do is execute via dtexec and it probably uses the 64bit dtexec by default.
<li>If you plan on opening BIDS on your server - refer to point 1 for the server as well because if you run BIDS on it, it's a development box.
<li>Avoid User DSNs.  While they are kind of magic (they don't differentiate between the 64bit and 32bit administration tool - there's only ONE list of User DSNs), they are a formula for headaches on shared development machines, packages ran by the sql server agent, or packages run via scheduled task (all depending on what user context you have setup on each engine).
<li>If you can't setup 64bit System DSNs and have to use 32-bit System DSNs, ensure you have your BIDS solution set to use the 32bit runtime, ensure when you run the packages using dtexec, you use the 32bit version, and when you execute the package via SQL Server Agent, configure it for 32bit execution:
<p><a href="http://timlaqua.com/wp-content/uploads/2011/11/SQL-Server-Agent-Package-Execution-Options.png"><img src="http://timlaqua.com/wp-content/uploads/2011/11/SQL-Server-Agent-Package-Execution-Options.png" alt="" title="SQL Server Agent - Package Execution Options" width="493" height="408" class="alignnone size-full wp-image-836" /></a>
</ol>
]]></content:encoded>
			<wfw:commentRss>http://timlaqua.com/2011/11/ssis-bids-and-64bit-system-dsns-whos-on-first/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Consuming an Authenticated JSON Feed with SSIS</title>
		<link>http://timlaqua.com/2011/07/consuming-an-authenticated-json-feed-with-ssis/</link>
		<comments>http://timlaqua.com/2011/07/consuming-an-authenticated-json-feed-with-ssis/#comments</comments>
		<pubDate>Sun, 03 Jul 2011 14:53:55 +0000</pubDate>
		<dc:creator>Tim</dc:creator>
				<category><![CDATA[Scripts & Code]]></category>
		<category><![CDATA[bi]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[ssis]]></category>

		<guid isPermaLink="false">http://timlaqua.com/?p=656</guid>
		<description><![CDATA[For this post, we'll look in to one option for consuming a JSON data feed from a service provider's API. In my case, the provider is AtTask - a web based project management service. This sort of assignment/request is pretty common as various corners of your organization simply want to use the best tool for [...]]]></description>
			<content:encoded><![CDATA[<p>For this post, we'll look in to one option for consuming a JSON data feed from a service provider's API.  In my case, the provider is <a href="http?://www.attask.com/">AtTask</a> - a web based project management service.  This sort of assignment/request is pretty common as various corners of your organization simply want to use the best tool for the job.  Far too infrequently does anyone really look at things like how it connects to your current systems.  They say things like "we have a well documented API and a strong user community" - yeah, I've heard that a few thousand times.  JSON is neat if you're using JQuery.  It's horrible if you're trying to suck data down in to a SQL Server table.  Which is what we're going to do.</p>
<p>First, we wander over to the <a href="https://community.attask.com/attask-restful-api">AtTask API Documentation</a> and figure out how to authenticate.  Notice that every request needs a sessionID - and that sessionID is created by a request to the login url.  This means we'll have to make at least two requests - one to nab a sessionID and another to actually get the data that we want.<br />
<span id="more-656"></span><br />
First, create a new package and drop down all the basic objects we'll need:</p>
<ol>
<li>OLEDB Connection</li>
<li>SQL Task: BEGIN TRANSACTION</li>
<li>SQL Task: TRUNCATE (truncate target table)</li>
<li>Data Flow: Populate table</li>
<li>SQL Task: COMMIT TRANSACTION</li>
<li>SQL Task: ROLLBACK TRANSACTION (failure predicates from truncate and data flow)</li>
</ol>
<p><em>Suggested Control Flow</em><br />
<a href="http://timlaqua.com/wp-content/uploads/2011/07/ControlFlow_Complete.png"><img src="http://timlaqua.com/wp-content/uploads/2011/07/ControlFlow_Complete.png" alt="" title="ControlFlow_Complete" width="380" height="304" class="alignnone size-full wp-image-685" /></a></p>
<p>Because we're trying to maintain a single transaction through multiple executables, set the <strong>RetainSameConnection </strong>property of your OLEDB connection to True.  You certainly could just TRUNCATE and then run the data flow, I just wanted to avoid having the end user(s) hit an empty table mid-load (the API I was working with was a tad slow at times).</p>
<p>Now that you have the control flow under... control... jump in to the Data flow where the interesting stuff happens.  You'll need at least a Script Component Source and an OLEDB destination.</p>
<p><em>Suggested Data Flow</em><br />
<a href="http://timlaqua.com/wp-content/uploads/2011/07/DataFlow_Complete.png"><img src="http://timlaqua.com/wp-content/uploads/2011/07/DataFlow_Complete.png" alt="" title="DataFlow_Complete" width="148" height="142" class="alignnone size-full wp-image-686" /></a></p>
<p>I created two variables to hold the URLs I needed to both login to the API and make the actual request<br />
<a href="http://timlaqua.com/wp-content/uploads/2011/07/DataFlow_Variables.png"><img src="http://timlaqua.com/wp-content/uploads/2011/07/DataFlow_Variables.png" alt="" title="DataFlow_Variables" width="369" height="62" class="alignnone size-full wp-image-687" /></a></p>
<p>Open up the Script Component Source and add the variables to the component:<br />
<a href="http://timlaqua.com/wp-content/uploads/2011/07/ScriptComponent_ReadOnlyVariables.png"><img src="http://timlaqua.com/wp-content/uploads/2011/07/ScriptComponent_ReadOnlyVariables.png" alt="" title="ScriptComponent_ReadOnlyVariables" width="461" height="50" class="alignnone size-full wp-image-691" /></a></p>
<p>Then configure the output to match what you want to come out of the component:<br />
<a href="http://timlaqua.com/wp-content/uploads/2011/07/ScriptComponent_AddOutputColumns.png"><img src="http://timlaqua.com/wp-content/uploads/2011/07/ScriptComponent_AddOutputColumns.png" alt="" title="ScriptComponent_AddOutputColumns" width="497" height="349" class="alignnone size-full wp-image-690" /></a></p>
<p>Finally, edit the script and switch it to build for the .NET Framework 3.5:<br />
<em>Project Explorer > Properties</em><br />
<a href="http://timlaqua.com/wp-content/uploads/2011/07/ProjectExplorer_Properties.png"><img src="http://timlaqua.com/wp-content/uploads/2011/07/ProjectExplorer_Properties.png" alt="" title="ProjectExplorer_Properties" width="277" height="299" class="alignnone size-full wp-image-689" /></a></p>
<p><em>Select .NET 3.5</em><br />
<a href="http://timlaqua.com/wp-content/uploads/2011/07/SelectDotNetFramework35.png"><img src="http://timlaqua.com/wp-content/uploads/2011/07/SelectDotNetFramework35.png" alt="" title="SelectDotNetFramework35" width="384" height="195" class="alignnone size-full wp-image-692" /></a></p>
<p>Then Add references that we'll need for this script:<br />
<a href="http://timlaqua.com/wp-content/uploads/2011/07/ProjectExplorer_AddReference.png"><img src="http://timlaqua.com/wp-content/uploads/2011/07/ProjectExplorer_AddReference.png" alt="" title="ProjectExplorer_AddReference" width="292" height="173" class="alignnone size-full wp-image-688" /></a></p>
<p>Select Assemblies we'll need (System.Runtime.Serialization and System.ServiceModel.Web)<br />
<a href="http://timlaqua.com/wp-content/uploads/2011/07/AddReference_SelectAssemblies.png"><img src="http://timlaqua.com/wp-content/uploads/2011/07/AddReference_SelectAssemblies.png" alt="" title="AddReference_SelectAssemblies" width="464" height="382" class="alignnone size-full wp-image-684" /></a></p>
<p><strong>Make sure to Save All at this point!  You want to make sure that the changes you made to the project get saved, not just the changes to the main.cs file.</strong></p>
<p>And here's my completed script.  The specifics of how you need to login and what the returned data feeds look like will vary, but the basic flow will likely remain the same.</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>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Xml</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.Runtime.Serialization.Json</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Runtime.Serialization</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Text.RegularExpressions</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: #008080; font-style: italic;">// loginURL should be a url that returns JSON containing a sessionID</span>
        <span style="color: #FF0000;">string</span> loginURL <span style="color: #008000;">=</span> Variables.<span style="color: #0000FF;">loginURL</span><span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #008080; font-style: italic;">// Get the sessionID</span>
        HttpWebRequest request <span style="color: #008000;">=</span> <span style="color: #000000;">&#40;</span>HttpWebRequest<span style="color: #000000;">&#41;</span>WebRequest.<span style="color: #0000FF;">Create</span><span style="color: #000000;">&#40;</span>loginURL<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        request.<span style="color: #0000FF;">Timeout</span> <span style="color: #008000;">=</span> <span style="color: #FF0000;">300000</span><span style="color: #008000;">;</span>
        HttpWebResponse response <span style="color: #008000;">=</span> <span style="color: #000000;">&#40;</span>HttpWebResponse<span style="color: #000000;">&#41;</span>request.<span style="color: #0000FF;">GetResponse</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        StreamReader reader <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> StreamReader<span style="color: #000000;">&#40;</span>response.<span style="color: #0000FF;">GetResponseStream</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: #FF0000;">string</span> json <span style="color: #008000;">=</span> reader.<span style="color: #0000FF;">ReadToEnd</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        Match m <span style="color: #008000;">=</span> Regex.<span style="color: #0000FF;">Match</span><span style="color: #000000;">&#40;</span>json, <span style="color: #666666;">&quot;<span style="color: #008080; font-weight: bold;">\&quot;</span>sessionID<span style="color: #008080; font-weight: bold;">\&quot;</span>: <span style="color: #008080; font-weight: bold;">\&quot;</span>(.+?)<span style="color: #008080; font-weight: bold;">\&quot;</span>&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #FF0000;">string</span> sessionId <span style="color: #008000;">=</span> m.<span style="color: #0000FF;">Groups</span><span style="color: #000000;">&#91;</span><span style="color: #FF0000;">1</span><span style="color: #000000;">&#93;</span>.<span style="color: #0000FF;">Value</span><span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #008080; font-style: italic;">// Need to support pagination, api has a 2000 item limit per req</span>
        XmlNodeList items<span style="color: #008000;">;</span>
        <span style="color: #FF0000;">int</span> rowsPerPage <span style="color: #008000;">=</span> <span style="color: #FF0000;">1000</span><span style="color: #008000;">;</span>
        <span style="color: #FF0000;">int</span> page <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #008080; font-style: italic;">// Loop until we find the last page</span>
        <span style="color: #0600FF;">do</span> <span style="color: #000000;">&#123;</span>
            <span style="color: #008080; font-style: italic;">// The taskURL has 3 tokens:</span>
            <span style="color: #008080; font-style: italic;">// {0} = The first record to return for the page we're requesting</span>
            <span style="color: #008080; font-style: italic;">// {1} = The number of records to request</span>
            <span style="color: #008080; font-style: italic;">// {2} = The sessionID</span>
            <span style="color: #FF0000;">string</span> taskURL <span style="color: #008000;">=</span> <span style="color: #FF0000;">string</span>.<span style="color: #0000FF;">Format</span><span style="color: #000000;">&#40;</span>
                Variables.<span style="color: #0000FF;">taskURL</span>, 
                page <span style="color: #008000;">*</span> rowsPerPage <span style="color: #008000;">+</span> <span style="color: #FF0000;">1</span>, 
                rowsPerPage, 
                sessionId<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            request <span style="color: #008000;">=</span> <span style="color: #000000;">&#40;</span>HttpWebRequest<span style="color: #000000;">&#41;</span>WebRequest.<span style="color: #0000FF;">Create</span><span style="color: #000000;">&#40;</span>taskURL<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            response <span style="color: #008000;">=</span> <span style="color: #000000;">&#40;</span>HttpWebResponse<span style="color: #000000;">&#41;</span>request.<span style="color: #0000FF;">GetResponse</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
            <span style="color: #008080; font-style: italic;">// Toss the JSON response in to XML so we can work with it better</span>
            XmlDocument xd <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> XmlDocument<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            XmlDictionaryReader xr <span style="color: #008000;">=</span> JsonReaderWriterFactory.<span style="color: #0000FF;">CreateJsonReader</span><span style="color: #000000;">&#40;</span>
                response.<span style="color: #0000FF;">GetResponseStream</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>, 
                XmlDictionaryReaderQuotas.<span style="color: #0000FF;">Max</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            xr.<span style="color: #0000FF;">Read</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            xd.<span style="color: #0000FF;">LoadXml</span><span style="color: #000000;">&#40;</span>xr.<span style="color: #0000FF;">ReadOuterXml</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
            <span style="color: #008080; font-style: italic;">// Put each item returned in to a new Buffer row</span>
            items <span style="color: #008000;">=</span> xd.<span style="color: #0000FF;">DocumentElement</span>.<span style="color: #0000FF;">SelectNodes</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;/root/data/item&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #0600FF;">foreach</span> <span style="color: #000000;">&#40;</span>XmlNode item <span style="color: #0600FF;">in</span> items<span style="color: #000000;">&#41;</span>
            <span style="color: #000000;">&#123;</span>
                TaskDataBuffer.<span style="color: #0000FF;">AddRow</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                TaskDataBuffer.<span style="color: #0000FF;">ProjectId</span> <span style="color: #008000;">=</span> 
                    item.<span style="color: #0000FF;">SelectSingleNode</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;project/ID&quot;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Value</span><span style="color: #008000;">;</span>
                TaskDataBuffer.<span style="color: #0000FF;">ProjectName</span> <span style="color: #008000;">=</span> 
                    item.<span style="color: #0000FF;">SelectSingleNode</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;project/name&quot;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Value</span><span style="color: #008000;">;</span>
&nbsp;
                <span style="color: #008080; font-style: italic;">// This particular feed allows assignedTo to be empty/null</span>
                <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>item.<span style="color: #0000FF;">SelectSingleNode</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;assignedTo/name&quot;</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">!=</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span>
                <span style="color: #000000;">&#123;</span>
                    TaskDataBuffer.<span style="color: #0000FF;">TaskAssignedToName</span> <span style="color: #008000;">=</span> 
                        item.<span style="color: #0000FF;">SelectSingleNode</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;assignedTo/name&quot;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Value</span><span style="color: #008000;">;</span>
                <span style="color: #000000;">&#125;</span>
                <span style="color: #0600FF;">else</span>
                <span style="color: #000000;">&#123;</span>
                    TaskDataBuffer.<span style="color: #0000FF;">TaskAssignedToName_IsNull</span> <span style="color: #008000;">=</span> true<span style="color: #008000;">;</span>
                <span style="color: #000000;">&#125;</span>
&nbsp;
                TaskDataBuffer.<span style="color: #0000FF;">TaskId</span> <span style="color: #008000;">=</span> 
                    item.<span style="color: #0000FF;">SelectSingleNode</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;ID&quot;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Value</span><span style="color: #008000;">;</span>
                TaskDataBuffer.<span style="color: #0000FF;">TaskName</span> <span style="color: #008000;">=</span> 
                    item.<span style="color: #0000FF;">SelectSingleNode</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;name&quot;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Value</span><span style="color: #008000;">;</span>
                TaskDataBuffer.<span style="color: #0000FF;">TaskPlannedCompletionDate</span> <span style="color: #008000;">=</span> 
                    item.<span style="color: #0000FF;">SelectSingleNode</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;plannedCompletionDate&quot;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Value</span><span style="color: #008000;">;</span>
                TaskDataBuffer.<span style="color: #0000FF;">TaskPlannedStartDate</span> <span style="color: #008000;">=</span> 
                    item.<span style="color: #0000FF;">SelectSingleNode</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;plannedStartDate&quot;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Value</span><span style="color: #008000;">;</span>
                TaskDataBuffer.<span style="color: #0000FF;">TaskProgressStatus</span> <span style="color: #008000;">=</span> 
                    item.<span style="color: #0000FF;">SelectSingleNode</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;progressStatus&quot;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Value</span><span style="color: #008000;">;</span>
            <span style="color: #000000;">&#125;</span>
            page<span style="color: #008000;">++;</span>
        <span style="color: #000000;">&#125;</span> <span style="color: #0600FF;">while</span> <span style="color: #000000;">&#40;</span>items.<span style="color: #0000FF;">Count</span> <span style="color: #008000;">==</span> rowsPerPage<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>Remember that each API you interface with will have it's own nuances to deal with and the above code is pretty specific for the AtTask API I was working with.  The part I want to point out is after we do all the API busy work, we have that JSON feed in a stream, turn it in to XML, and then access the data using XPath selects.</p>
]]></content:encoded>
			<wfw:commentRss>http://timlaqua.com/2011/07/consuming-an-authenticated-json-feed-with-ssis/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Revisiting Embedded Text Qualifiers and the SSIS Flat File Connection Manager</title>
		<link>http://timlaqua.com/2011/06/revisiting-embedded-text-qualifiers-and-the-ssis-flat-file-connection-manager/</link>
		<comments>http://timlaqua.com/2011/06/revisiting-embedded-text-qualifiers-and-the-ssis-flat-file-connection-manager/#comments</comments>
		<pubDate>Thu, 09 Jun 2011 01:45:42 +0000</pubDate>
		<dc:creator>Tim</dc:creator>
				<category><![CDATA[Scripts & Code]]></category>
		<category><![CDATA[bi]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[powershell]]></category>
		<category><![CDATA[ssis]]></category>

		<guid isPermaLink="false">http://timlaqua.com/?p=607</guid>
		<description><![CDATA[To quickly summarize the problem at hand, the Flat File Connection Manager doesn't support embedded Text Qualifiers. The reason this is a big issue is because the most commonly used text qualifier is a double quote (&#34;) and that's also a commonly used punctuation mark. That means if you have a CSV to import and [...]]]></description>
			<content:encoded><![CDATA[<p>To quickly summarize the problem at hand, the Flat File Connection Manager doesn't support embedded Text Qualifiers.  The reason this is a big issue is because the most commonly used text qualifier is a double quote (&quot;) and that's also a commonly used punctuation mark.  That means if you have a CSV to import and lines that looks like this:<br />
<code><br />
"one","1","two","2","th""r""ee","3","asdf"<br />
"""left","right""","mid""dle","w"",tf","w,""tf",",""omg","omg"","<br />
</code></p>
<p>You would expect to import</p>
<table>
<tr>
<td>one</td>
<td>1</td>
<td>two</td>
<td>2</td>
<td>th"r"ee</td>
<td>3</td>
<td>asdf</td>
</tr>
<tr>
<td>"left</td>
<td>right"</td>
<td>mid"dle</td>
<td>w",tf</td>
<td>w,"tf</td>
<td>,"omg</td>
<td>omg",</td>
</tr>
</table>
<p>That's not what you get, you get an error.  I've seen a few different approaches to working around this known issue:</p>
<ul>
<li><a href="http://www.andrewdenhertog.com/tips/importing-csv-flat-files-in-ssis-dealing-with-double-quotes">Replacing embedded text qualifiers with some other token - &amp;quot; in this case</a></li>
<li><a href="http://www.ideaexcursion.com/2008/11/12/handling-embedded-text-qualifiers/">Import each file as a single column and parse the row in a script task, handling error rows</a></li>
</ul>
<p>What I'd like to explore in this post is using regular expressions to transform the entire file in to something that can be natively consumed by SSIS.  There are two options for dealing with this:</p>
<ul>
<li>Tokenizing the quotation marks found within the cells</li>
<li>Change the text qualifier</li>
</ul>
<p>I chose to change the text qualifier because SSIS natively supports multiple character text qualifiers.  This will allow me to pick something ridiculous that is incredibly unlikely to appear in the data naturally.  Changing the text qualifier also means we won't have to add any extra transformations as we would if we tokenized the embeded quotation marks (to turn them back in to quotation marks).<br />
<span id="more-607"></span><br />
Let's start out with the file we're importing so we have some text to test against (C:\SSISTextQualifier\Input.csv)</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">&quot;one&quot;,&quot;1&quot;,&quot;two&quot;,&quot;2&quot;,&quot;th&quot;&quot;r&quot;&quot;ee&quot;,&quot;3&quot;,&quot;asdf&quot;
&quot;&quot;&quot;left&quot;,&quot;right&quot;&quot;&quot;,&quot;mid&quot;&quot;dle&quot;,&quot;w&quot;&quot;,tf&quot;,&quot;w,&quot;&quot;tf&quot;,&quot;,&quot;&quot;omg&quot;,&quot;omg&quot;&quot;,&quot;</pre></div></div>

<p>Now let's iron out the regular expression we're going to need.</p>
<p>We'll start out with grabbing a bunch of stuff between two double-quotes<br/><br />
<a href="http://timlaqua.com/wp-content/uploads/2011/06/Regex07.png"><img src="http://timlaqua.com/wp-content/uploads/2011/06/Regex07.png" alt="" title="Regex07" width="620" height="343" class="alignnone size-full wp-image-624" /></a><br />
<br/><br />
Ok, we don't just want a bunch of stuff - we want the stuff between each set of quotation marks - we make the * quantifier lazy with a question mark<br/><br />
<a href="http://timlaqua.com/wp-content/uploads/2011/06/Regex01.png"><img src="http://timlaqua.com/wp-content/uploads/2011/06/Regex01.png" alt="" title="Regex01" width="620" height="315" class="alignnone size-full wp-image-616" /></a><br />
<br/><br />
Better... but that th""r""ee is clearly wrong... we have to stop the final quotation mark from matching properly escaped quotation marks.  We can do that with a negative lookahead:<br/><br />
<a href="http://timlaqua.com/wp-content/uploads/2011/06/Regex12.png"><img src="http://timlaqua.com/wp-content/uploads/2011/06/Regex12.png" alt="" title="Regex12" width="620" height="343" class="alignnone size-full wp-image-677" /></a><br />
<br/><br />
That's different - notice that now properly escaped quotation marks aren't abnormally terminating the column match anymore, now they're just acting like normal characters.  We have to make a special exception for escaped quotation marks.  Those have to essentially count as a single character:<br/><br />
<a href="http://timlaqua.com/wp-content/uploads/2011/06/Regex08.png"><img src="http://timlaqua.com/wp-content/uploads/2011/06/Regex08.png" alt="" title="Regex08" width="620" height="343" class="alignnone size-full wp-image-627" /></a><br />
<br/><br />
Perfect - Now we can replace the text qualifiers with something awesome like |~| since we have matched the column content safely<br/><br />
<a href="http://timlaqua.com/wp-content/uploads/2011/06/Regex05.png"><img src="http://timlaqua.com/wp-content/uploads/2011/06/Regex05.png" alt="" title="Regex05" width="620" height="315" class="alignnone size-full wp-image-621" /></a></p>
<p>Now that we have our regex, we just have to decide how to execute.</p>
<h3>Plan A:  SSIS Script Task</h3>
<p>Create a new package with a single script task in it<br/><br />
<a href="http://timlaqua.com/wp-content/uploads/2011/06/SSIS01.png"><img src="http://timlaqua.com/wp-content/uploads/2011/06/SSIS01.png" alt="" title="SSIS01" width="660" height="418" class="alignnone size-full wp-image-637" /></a></p>
<p>Now edit the script (C#) and add the following up by the existing using statements:</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.IO</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.RegularExpressions</span><span style="color: #008000;">;</span></pre></div></div>

<p>Modify the Main method to look something like this (depending on your paths):</p>

<div class="wp_syntax"><div 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>
            StreamReader sr <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> <span style="color: #000000;">System.<span style="color: #0000FF;">IO</span></span>.<span style="color: #0000FF;">StreamReader</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">@&quot;C:\SSISTextQualifier\Input.csv&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #FF0000;">string</span> allText <span style="color: #008000;">=</span> sr.<span style="color: #0000FF;">ReadToEnd</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            sr.<span style="color: #0000FF;">Close</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            StreamWriter sw <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> StreamWriter<span style="color: #000000;">&#40;</span><span style="color: #666666;">@&quot;C:\SSISTextQualifier\InputCleaned.csv&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            sw.<span style="color: #0000FF;">Write</span><span style="color: #000000;">&#40;</span>
                    Regex.<span style="color: #0000FF;">Replace</span><span style="color: #000000;">&#40;</span>
                        allText, 
                        <span style="color: #666666;">@&quot;&quot;</span><span style="color: #666666;">&quot;((?:&quot;</span><span style="color: #666666;">&quot;&quot;</span><span style="color: #666666;">&quot;|.)*?)&quot;</span><span style="color: #666666;">&quot;(?!&quot;</span><span style="color: #666666;">&quot;)&quot;</span>, 
                        <span style="color: #666666;">@&quot;|~|$1|~|&quot;</span>
                    <span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Replace</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;<span style="color: #008080; font-weight: bold;">\&quot;</span><span style="color: #008080; font-weight: bold;">\&quot;</span>&quot;</span>, <span style="color: #666666;">&quot;<span style="color: #008080; font-weight: bold;">\&quot;</span>&quot;</span><span style="color: #000000;">&#41;</span>
            <span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            sw.<span style="color: #0000FF;">Close</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            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>
        <span style="color: #000000;">&#125;</span></pre></div></div>

<p>Then run that task and you will have a new file called C:\SSISTextQualifier\InputCleaned.csv that's text qualified by |~| and the escaped double quotes have been replaced with a single double quote.</p>
<h3>Plan B: Powershell</h3>
<p>Since we often call SSIS packages from the SQL Server Agent, we can easily add a Powershell step right before the SSIS package to copy and cleanse our file.  If that's the route you choose, your powershell step will look something like this:</p>

<div class="wp_syntax"><div class="code"><pre class="powershell" style="font-family:monospace;"><span style="color: #008080; font-weight: bold;">gc</span> C:\SSISTextQualifier\Input.csv <span style="color: pink;">|</span> 
<span style="color: pink;">%</span> <span style="color: #000000;">&#123;</span><span style="color: #800080;"><span style="color: #000080;">$_</span></span> <span style="color: #FF0000;">-replace</span> <span style="color: #800000;">&quot;<span style="color: #008080; font-weight: bold;">`&quot;</span>((?:<span style="color: #008080; font-weight: bold;">`&quot;</span><span style="color: #008080; font-weight: bold;">`&quot;</span>|.)*?)<span style="color: #008080; font-weight: bold;">`&quot;</span>(?!<span style="color: #008080; font-weight: bold;">`&quot;</span>)&quot;</span><span style="color: pink;">,</span> <span style="color: #800000;">&quot;|~|<span style="color: #008080; font-weight: bold;">`$</span>1|~|&quot;</span> <span style="color: #FF0000;">-replace</span> <span style="color: #800000;">&quot;<span style="color: #008080; font-weight: bold;">`&quot;</span><span style="color: #008080; font-weight: bold;">`&quot;</span>&quot;</span><span style="color: pink;">,</span> <span style="color: #800000;">&quot;<span style="color: #008080; font-weight: bold;">`&quot;</span>&quot;</span><span style="color: #000000;">&#125;</span> <span style="color: pink;">|</span> 
<span style="color: #008080; font-weight: bold;">sc</span> C:\SSISTextQualifier\InputCleaned2.csv</pre></div></div>

<h3>Final Steps</h3>
<p>However you got your data in to a new file (<strong>C:\SSISTextQualifier\InputCleaned.csv</strong> in my case) with a new text qualifier, you still have to then import the file.  This is done using our old friend the Flat File Connection Manager with the source being the newly minted clean file and with our new text qualifier set as |~|</p>
<p><a href="http://timlaqua.com/wp-content/uploads/2011/06/SSIS02.png"><img src="http://timlaqua.com/wp-content/uploads/2011/06/SSIS02.png" alt="" title="SSIS02" width="550" height="562" class="alignnone size-full wp-image-642" /></a></p>
<p>And that's pretty much it:<br/><br />
<a href="http://timlaqua.com/wp-content/uploads/2011/06/SSIS03.png"><img src="http://timlaqua.com/wp-content/uploads/2011/06/SSIS03.png" alt="" title="SSIS03" width="550" height="562" class="alignnone size-full wp-image-643" /></a></p>
<h3>Conclusion</h3>
<p>Personally, I think the Powershell plan is the cleanest and easiest to implement.  The code isn't hidden away in a script task, and it looks cool.  Doing it this way allows us to kind of abstract this "file fixing" from the SSIS data flows and let them do what they do best.</p>
]]></content:encoded>
			<wfw:commentRss>http://timlaqua.com/2011/06/revisiting-embedded-text-qualifiers-and-the-ssis-flat-file-connection-manager/feed/</wfw:commentRss>
		<slash:comments>0</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>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[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 [...]]]></description>
			<content:encoded><![CDATA[<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>
		<item>
		<title>Adding Windows Event Log Logging To Existing SSIS Packages via C#</title>
		<link>http://timlaqua.com/2009/03/adding-windows-event-log-logging-to-existing-ssis-packages-via-c/</link>
		<comments>http://timlaqua.com/2009/03/adding-windows-event-log-logging-to-existing-ssis-packages-via-c/#comments</comments>
		<pubDate>Wed, 01 Apr 2009 02:19:45 +0000</pubDate>
		<dc:creator>Tim</dc:creator>
				<category><![CDATA[Scripts & Code]]></category>
		<category><![CDATA[business intelligence]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[integration services]]></category>
		<category><![CDATA[ssis]]></category>

		<guid isPermaLink="false">http://timlaqua.com/?p=182</guid>
		<description><![CDATA[While working with some SSIS logging, it occured to me that there's really no good way to globally apply the same logging to all packages on your server. For our production SSIS server, it would make sense to log all OnError events to the Application Event log. Then, whatever event log monitoring app you use [...]]]></description>
			<content:encoded><![CDATA[<p>While working with some SSIS logging, it occured to me that there's really no good way to globally apply the same logging to all packages on your server.  For our production SSIS server, it would make sense to log all OnError events to the Application Event log.  Then, whatever event log monitoring app you use can notify you of the package error (with details!).  Thus, this app was born - an app to remove pre-existing LogProviders and add in the intended LogProvider and options.</p>
<p>This is really just an example of the implementation that worked for my purposes - I'm sure some of you would prefer to log elsewhere.  This should be enough to get you going.  Please drop a line in the Comments if you post alternative approaches.</p>
<p><u>Usage</u></p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">SSISForcedLogging.Console.exe &quot;Z:\SSIS Packages&quot;
or
SSISForcedLogging.Console.Exe &quot;Z:\SSIS Packages\Package1.dtsx&quot;</pre></div></div>

<p><span id="more-182"></span><br />
For the following code, be sure to add References to:</p>
<ul>
<li>Microsoft.SqlServer.ManagedDTS</li>
<li>System.configuration</li>
</ul>
<p><u>Program.cs</u></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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
</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.Configuration</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">Microsoft.SqlServer.Dts.Runtime</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Reflection</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Collections</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.Collections.Generic</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #0600FF;">namespace</span> SSISForcedLogging.<span style="color: #0000FF;">Console</span>
<span style="color: #000000;">&#123;</span>
    <span style="color: #FF0000;">class</span> ColumnFilter
    <span style="color: #000000;">&#123;</span>
        <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">bool</span> Computer<span style="color: #008000;">;</span>
        <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">bool</span> DataBytes<span style="color: #008000;">;</span>
        <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">bool</span> ExecutionID<span style="color: #008000;">;</span>
        <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">bool</span> MessageText<span style="color: #008000;">;</span>
        <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">bool</span> Operator<span style="color: #008000;">;</span>
        <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">bool</span> SourceID<span style="color: #008000;">;</span>
        <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">bool</span> SourceName<span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #0600FF;">public</span> DTSEventColumnFilter GetDTSEventColumnFilter<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            DTSEventColumnFilter dtsEventColumnFilter <span style="color: #008000;">=</span> 
                <span style="color: #008000;">new</span> DTSEventColumnFilter<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            dtsEventColumnFilter.<span style="color: #0000FF;">DataBytes</span> <span style="color: #008000;">=</span> <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">DataBytes</span><span style="color: #008000;">;</span>
            dtsEventColumnFilter.<span style="color: #0000FF;">Computer</span> <span style="color: #008000;">=</span> <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">Computer</span><span style="color: #008000;">;</span>
            dtsEventColumnFilter.<span style="color: #0000FF;">ExecutionID</span> <span style="color: #008000;">=</span> <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">ExecutionID</span><span style="color: #008000;">;</span>
            dtsEventColumnFilter.<span style="color: #0000FF;">MessageText</span> <span style="color: #008000;">=</span> <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">MessageText</span><span style="color: #008000;">;</span>
            dtsEventColumnFilter.<span style="color: #0600FF;">Operator</span> <span style="color: #008000;">=</span> <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">Operator</span><span style="color: #008000;">;</span>
            dtsEventColumnFilter.<span style="color: #0000FF;">SourceID</span> <span style="color: #008000;">=</span> <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">SourceID</span><span style="color: #008000;">;</span>
            dtsEventColumnFilter.<span style="color: #0000FF;">SourceName</span> <span style="color: #008000;">=</span> <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">SourceName</span><span style="color: #008000;">;</span>
            <span style="color: #0600FF;">return</span> dtsEventColumnFilter<span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #FF0000;">class</span> Program
    <span style="color: #000000;">&#123;</span>
        <span style="color: #0600FF;">static</span> <span style="color: #0600FF;">void</span> Main<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> args<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>args.<span style="color: #0000FF;">Length</span> <span style="color: #008000;">!=</span> <span style="color: #FF0000;">1</span><span style="color: #000000;">&#41;</span>
            <span style="color: #000000;">&#123;</span>
                <span style="color: #000000;">System</span>.<span style="color: #0000FF;">Console</span>.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#40;</span>
                    <span style="color: #666666;">&quot;<span style="color: #008080; font-weight: bold;">\n</span>Please specify a package path or directory<span style="color: #008080; font-weight: bold;">\n</span>&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                Environment.<span style="color: #0000FF;">Exit</span><span style="color: #000000;">&#40;</span><span style="color: #008000;">-</span><span style="color: #FF0000;">1</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #000000;">&#125;</span>
&nbsp;
            <span style="color: #FF0000;">string</span> arg <span style="color: #008000;">=</span> args<span style="color: #000000;">&#91;</span><span style="color: #FF0000;">0</span><span style="color: #000000;">&#93;</span><span style="color: #008000;">;</span>
            List<span style="color: #008000;">&lt;</span><span style="color: #FF0000;">string</span><span style="color: #008000;">&gt;</span> packages <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> List<span style="color: #008000;">&lt;</span><span style="color: #FF0000;">string</span><span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
            <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>File.<span style="color: #0000FF;">Exists</span><span style="color: #000000;">&#40;</span>arg<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
                packages.<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span>arg<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #0600FF;">else</span> <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>Directory.<span style="color: #0000FF;">Exists</span><span style="color: #000000;">&#40;</span>arg<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
                packages <span style="color: #008000;">=</span> GetPackagesFromDirectory<span style="color: #000000;">&#40;</span>arg<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #0600FF;">else</span>
            <span style="color: #000000;">&#123;</span>
                <span style="color: #000000;">System</span>.<span style="color: #0000FF;">Console</span>.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#40;</span>
                    <span style="color: #666666;">&quot;<span style="color: #008080; font-weight: bold;">\n</span>Invalid package path or directory specified<span style="color: #008080; font-weight: bold;">\n</span>&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                Environment.<span style="color: #0000FF;">Exit</span><span style="color: #000000;">&#40;</span><span style="color: #008000;">-</span><span style="color: #FF0000;">1</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #000000;">&#125;</span>
&nbsp;
            <span style="color: #0600FF;">foreach</span> <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span> pkgPath <span style="color: #0600FF;">in</span> packages<span style="color: #000000;">&#41;</span>
            <span style="color: #000000;">&#123;</span>
                <span style="color: #000000;">System</span>.<span style="color: #0000FF;">Console</span>.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#40;</span>
                    <span style="color: #FF0000;">string</span>.<span style="color: #0000FF;">Format</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Updating: {0}&quot;</span>, pkgPath<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                Application dts <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Application<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                Package pkg <span style="color: #008000;">=</span> dts.<span style="color: #0000FF;">LoadPackage</span><span style="color: #000000;">&#40;</span>pkgPath, <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
                RemoveOldLogsFromPackage<span style="color: #000000;">&#40;</span><span style="color: #0600FF;">ref</span> pkg<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                AddLogToPackage<span style="color: #000000;">&#40;</span><span style="color: #0600FF;">ref</span> pkg<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
                <span style="color: #000000;">System</span>.<span style="color: #0000FF;">Console</span>.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#40;</span>
                    <span style="color: #FF0000;">string</span>.<span style="color: #0000FF;">Format</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Saving: {0}&quot;</span>, pkgPath<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                dts.<span style="color: #0000FF;">SaveToXml</span><span style="color: #000000;">&#40;</span>pkgPath, pkg, <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #000000;">&#125;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #0600FF;">void</span> AddLogToPackage<span style="color: #000000;">&#40;</span><span style="color: #0600FF;">ref</span> Package Pkg<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #008080; font-style: italic;">// Add new provider</span>
            Pkg.<span style="color: #0000FF;">LoggingMode</span> <span style="color: #008000;">=</span> DTSLoggingMode.<span style="color: #0000FF;">Enabled</span><span style="color: #008000;">;</span>
&nbsp;
            LogProvider log <span style="color: #008000;">=</span> Pkg.<span style="color: #0000FF;">LogProviders</span>.<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span>
                ConfigurationManager.<span style="color: #0000FF;">AppSettings</span><span style="color: #000000;">&#91;</span><span style="color: #666666;">&quot;logType&quot;</span><span style="color: #000000;">&#93;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            log.<span style="color: #0000FF;">Name</span> <span style="color: #008000;">=</span> 
                ConfigurationManager.<span style="color: #0000FF;">AppSettings</span><span style="color: #000000;">&#91;</span><span style="color: #666666;">&quot;logName&quot;</span><span style="color: #000000;">&#93;</span><span style="color: #008000;">;</span>
            log.<span style="color: #0000FF;">Description</span> <span style="color: #008000;">=</span> 
                ConfigurationManager.<span style="color: #0000FF;">AppSettings</span><span style="color: #000000;">&#91;</span><span style="color: #666666;">&quot;logDesc&quot;</span><span style="color: #000000;">&#93;</span><span style="color: #008000;">;</span>
            Pkg.<span style="color: #0000FF;">LoggingOptions</span>.<span style="color: #0000FF;">SelectedLogProviders</span>.<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span>log<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
            LoggingOptions logOptions <span style="color: #008000;">=</span> Pkg.<span style="color: #0000FF;">LoggingOptions</span><span style="color: #008000;">;</span>
            logOptions.<span style="color: #0000FF;">EventFilterKind</span> <span style="color: #008000;">=</span> DTSEventFilterKind.<span style="color: #0000FF;">Inclusion</span><span style="color: #008000;">;</span>
&nbsp;
            <span style="color: #FF0000;">string</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> events <span style="color: #008000;">=</span> 
                ConfigurationManager
                    .<span style="color: #0000FF;">AppSettings</span><span style="color: #000000;">&#91;</span><span style="color: #666666;">&quot;eventsToLog&quot;</span><span style="color: #000000;">&#93;</span>
                        .<span style="color: #0000FF;">Split</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">','</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            logOptions.<span style="color: #0000FF;">EventFilter</span> <span style="color: #008000;">=</span> events<span style="color: #008000;">;</span>
&nbsp;
            ArrayList columnList <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> ArrayList<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            columnList.<span style="color: #0000FF;">AddRange</span><span style="color: #000000;">&#40;</span>
                ConfigurationManager
                    .<span style="color: #0000FF;">AppSettings</span><span style="color: #000000;">&#91;</span><span style="color: #666666;">&quot;columnsToLog&quot;</span><span style="color: #000000;">&#93;</span>
                        .<span style="color: #0000FF;">Split</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">','</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
            ColumnFilter eventColumnFilter <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> ColumnFilter<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
            <span style="color: #0600FF;">foreach</span> <span style="color: #000000;">&#40;</span>FieldInfo field <span style="color: #0600FF;">in</span> 
                     eventColumnFilter.<span style="color: #0000FF;">GetType</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">GetFields</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
                <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>columnList.<span style="color: #0000FF;">Contains</span><span style="color: #000000;">&#40;</span>field.<span style="color: #0000FF;">Name</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
                    field.<span style="color: #0000FF;">SetValue</span><span style="color: #000000;">&#40;</span>eventColumnFilter, <span style="color: #0600FF;">true</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
            logOptions.<span style="color: #0000FF;">SetColumnFilter</span><span style="color: #000000;">&#40;</span>
                <span style="color: #666666;">&quot;OnError&quot;</span>, 
                eventColumnFilter.<span style="color: #0000FF;">GetDTSEventColumnFilter</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #0600FF;">void</span> RemoveOldLogsFromPackage<span style="color: #000000;">&#40;</span><span style="color: #0600FF;">ref</span> Package Pkg<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #008080; font-style: italic;">// Remove existing log provider selections</span>
            <span style="color: #FF0000;">int</span> count <span style="color: #008000;">=</span> Pkg.<span style="color: #0000FF;">LoggingOptions</span>.<span style="color: #0000FF;">SelectedLogProviders</span>.<span style="color: #0000FF;">Count</span><span style="color: #008000;">;</span>
            <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> count<span style="color: #008000;">;</span> i<span style="color: #008000;">++</span><span style="color: #000000;">&#41;</span>
                Pkg.<span style="color: #0000FF;">LoggingOptions</span>.<span style="color: #0000FF;">SelectedLogProviders</span>.<span style="color: #0000FF;">Remove</span><span style="color: #000000;">&#40;</span>i<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
            <span style="color: #008080; font-style: italic;">// Remove existing log providers</span>
            count <span style="color: #008000;">=</span> Pkg.<span style="color: #0000FF;">LogProviders</span>.<span style="color: #0000FF;">Count</span><span style="color: #008000;">;</span>
            <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> count<span style="color: #008000;">;</span> i<span style="color: #008000;">++</span><span style="color: #000000;">&#41;</span>
                Pkg.<span style="color: #0000FF;">LogProviders</span>.<span style="color: #0000FF;">Remove</span><span style="color: #000000;">&#40;</span>i<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
            RemoveOldLogsFromExecutable<span style="color: #000000;">&#40;</span>Pkg.<span style="color: #0000FF;">Executables</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #0600FF;">void</span> RemoveOldLogsFromExecutable<span style="color: #000000;">&#40;</span>Executables Execs<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #008080; font-style: italic;">// reflection ftw</span>
            <span style="color: #FF0000;">int</span> count<span style="color: #008000;">;</span>
            <span style="color: #0600FF;">foreach</span><span style="color: #000000;">&#40;</span>Executable exec <span style="color: #0600FF;">in</span> Execs<span style="color: #000000;">&#41;</span> 
            <span style="color: #000000;">&#123;</span>
                <span style="color: #0600FF;">foreach</span> <span style="color: #000000;">&#40;</span>PropertyInfo prop <span style="color: #0600FF;">in</span> 
                         exec.<span style="color: #0000FF;">GetType</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">GetProperties</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
                <span style="color: #000000;">&#123;</span>
                    <span style="color: #0600FF;">switch</span><span style="color: #000000;">&#40;</span>prop.<span style="color: #0000FF;">Name</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
                        <span style="color: #0600FF;">case</span> <span style="color: #666666;">&quot;LoggingMode&quot;</span><span style="color: #008000;">:</span>
                            prop.<span style="color: #0000FF;">SetValue</span><span style="color: #000000;">&#40;</span>
                                exec, 
                                DTSLoggingMode.<span style="color: #0000FF;">UseParentSetting</span>, 
                                <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                            break<span style="color: #008000;">;</span>
                        <span style="color: #0600FF;">case</span> <span style="color: #666666;">&quot;LoggingOptions&quot;</span><span style="color: #008000;">:</span>
                            LoggingOptions opts <span style="color: #008000;">=</span> 
                                prop.<span style="color: #0000FF;">GetValue</span><span style="color: #000000;">&#40;</span>
                                    exec, 
                                    <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span> <span style="color: #0600FF;">as</span> LoggingOptions<span style="color: #008000;">;</span>
&nbsp;
                            count <span style="color: #008000;">=</span> opts.<span style="color: #0000FF;">SelectedLogProviders</span>.<span style="color: #0000FF;">Count</span><span style="color: #008000;">;</span>
                            <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> count<span style="color: #008000;">;</span> i<span style="color: #008000;">++</span><span style="color: #000000;">&#41;</span>
                                opts.<span style="color: #0000FF;">SelectedLogProviders</span>.<span style="color: #0000FF;">Remove</span><span style="color: #000000;">&#40;</span>i<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                            break<span style="color: #008000;">;</span>
                        <span style="color: #0600FF;">case</span> <span style="color: #666666;">&quot;Executables&quot;</span><span style="color: #008000;">:</span>
                            Executables execs <span style="color: #008000;">=</span> 
                                prop.<span style="color: #0000FF;">GetValue</span><span style="color: #000000;">&#40;</span>
                                exec, 
                                <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span> <span style="color: #0600FF;">as</span> Executables<span style="color: #008000;">;</span>
                            RemoveOldLogsFromExecutable<span style="color: #000000;">&#40;</span>execs<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                            break<span style="color: #008000;">;</span>
                    <span style="color: #000000;">&#125;</span>
                <span style="color: #000000;">&#125;</span>
            <span style="color: #000000;">&#125;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> List<span style="color: #008000;">&lt;</span><span style="color: #FF0000;">string</span><span style="color: #008000;">&gt;</span> GetPackagesFromDirectory<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span> Path<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            List<span style="color: #008000;">&lt;</span><span style="color: #FF0000;">string</span><span style="color: #008000;">&gt;</span> files <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> List<span style="color: #008000;">&lt;</span><span style="color: #FF0000;">string</span><span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
            <span style="color: #0600FF;">foreach</span> <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span> d <span style="color: #0600FF;">in</span> Directory.<span style="color: #0000FF;">GetDirectories</span><span style="color: #000000;">&#40;</span>Path<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
                <span style="color: #0600FF;">foreach</span> <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span> f <span style="color: #0600FF;">in</span> GetPackagesFromDirectory<span style="color: #000000;">&#40;</span>d<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
                    files.<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span>f<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
            <span style="color: #0600FF;">foreach</span> <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span> f <span style="color: #0600FF;">in</span> Directory.<span style="color: #0000FF;">GetFiles</span><span style="color: #000000;">&#40;</span>Path, <span style="color: #666666;">&quot;*.dtsx&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
                files.<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span>f<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
            <span style="color: #0600FF;">return</span> files<span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p><u>App.config</u></p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;utf-8&quot;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;configuration<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;appSettings<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;add</span> <span style="color: #000066;">key</span>=<span style="color: #ff0000;">&quot;logType&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;DTS.LogProviderEventLog.1&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;add</span> <span style="color: #000066;">key</span>=<span style="color: #ff0000;">&quot;logName&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;Windows Event Log Logging Provider (Forced)&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;add</span> <span style="color: #000066;">key</span>=<span style="color: #ff0000;">&quot;logDesc&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;Logs Event info to Windows Event Log (Forced)&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;add</span> <span style="color: #000066;">key</span>=<span style="color: #ff0000;">&quot;eventsToLog&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;OnError&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;add</span> <span style="color: #000066;">key</span>=<span style="color: #ff0000;">&quot;columnsToLog&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;Computer,Operator,SourceName,SourceID,ExecutionID,MessageText&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
	   <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/appSettings<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/configuration<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://timlaqua.com/2009/03/adding-windows-event-log-logging-to-existing-ssis-packages-via-c/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Exporting SSIS Packages Stored on the Server (MSDB) to .DTSX (XML)</title>
		<link>http://timlaqua.com/2008/06/exporting-ssis-packages-stored-on-the-server-msdb-to-dtsx-xml/</link>
		<comments>http://timlaqua.com/2008/06/exporting-ssis-packages-stored-on-the-server-msdb-to-dtsx-xml/#comments</comments>
		<pubDate>Fri, 27 Jun 2008 17:10:34 +0000</pubDate>
		<dc:creator>Tim</dc:creator>
				<category><![CDATA[Scripts & Code]]></category>
		<category><![CDATA[bi]]></category>
		<category><![CDATA[business intelligence]]></category>
		<category><![CDATA[dtsx]]></category>
		<category><![CDATA[msdb]]></category>
		<category><![CDATA[package]]></category>
		<category><![CDATA[ssis]]></category>

		<guid isPermaLink="false">http://timlaqua.com/?p=33</guid>
		<description><![CDATA[We wanted to export an SSIS package that was stored on the server in the msdb.dbo.sysdtspackages90 table to a .dtsx file so we could poke at it. Here's what we came up with: DECLARE @SQLcommand VARCHAR&#40;MAX&#41; SET @SQLcommand = 'bcp &#34;SELECT cast(cast(packagedata as varbinary(max)) as varchar(max)) FROM msdb.dbo.sysdtspackages90 WHERE name=''PackageName''&#34; queryout &#34;c:\output.dtsx&#34; -T -c' EXEC [...]]]></description>
			<content:encoded><![CDATA[<p>We wanted to export an SSIS package that was stored on the server in the msdb.dbo.sysdtspackages90 table to a .dtsx file so we could poke at it. </p>
<p>Here's what we came up with:</p>

<div class="wp_syntax"><div class="code"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">DECLARE</span> @SQLcommand <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">MAX</span><span style="color: #808080;">&#41;</span>
<span style="color: #0000FF;">SET</span> @SQLcommand <span style="color: #808080;">=</span> 
    <span style="color: #FF0000;">'bcp &quot;SELECT cast(cast(packagedata as varbinary(max)) as varchar(max)) 
     FROM msdb.dbo.sysdtspackages90 
     WHERE name='</span><span style="color: #FF0000;">'PackageName'</span><span style="color: #FF0000;">'&quot; queryout &quot;c:<span style="color: #000099; font-weight: bold;">\o</span>utput.dtsx&quot; -T -c'</span>
<span style="color: #0000FF;">EXEC</span> xp_cmdshell @SQLcommand</pre></div></div>

<p>Alternatively, you can just Export the package via SSMS <img src='http://timlaqua.com/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://timlaqua.com/2008/06/exporting-ssis-packages-stored-on-the-server-msdb-to-dtsx-xml/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

