<?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>youdidwhatwithtsql.com &#187; SSIS</title>
	<atom:link href="http://www.youdidwhatwithtsql.com/category/ssis/feed" rel="self" type="application/rss+xml" />
	<link>http://www.youdidwhatwithtsql.com</link>
	<description>making DBAs everywhere curse!</description>
	<lastBuildDate>Tue, 31 Jan 2012 12:21:23 +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: Loading files into MySQL</title>
		<link>http://www.youdidwhatwithtsql.com/ssis-loading-files-into-mysql/745</link>
		<comments>http://www.youdidwhatwithtsql.com/ssis-loading-files-into-mysql/745#comments</comments>
		<pubDate>Sat, 17 Apr 2010 14:01:01 +0000</pubDate>
		<dc:creator>Rhys</dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[SSIS]]></category>

		<guid isPermaLink="false">http://www.youdidwhatwithtsql.com/ssis-loading-files-into-mysql/745</guid>
		<description><![CDATA[Getting data out of MySQL with SSIS is a snap. Putting data into MySQL has been a different matter. I&#8217;ve always done this in the past with a hodgepodge of ODBC, Linked Servers, OPENQUERY and the Script Task. All of these work well but they&#8217;re just not as convenient as loading files with the OLED [...]<p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/ssis-loading-files-into-mysql/745">SSIS: Loading files into MySQL</a></p>
]]></description>
			<content:encoded><![CDATA[<p>Getting data out of <a href="http://www.mysql.com" target="_blank">MySQL</a> with <a href="http://www.microsoft.com/sqlserver/2005/en/us/integration-services.aspx" target="_blank">SSIS</a> is a snap. Putting data into MySQL has been a different matter. I&#8217;ve always done this in the past with a <a href="http://en.wikipedia.org/wiki/Hodge-podge" target="_blank">hodgepodge</a> of <a href="http://en.wikipedia.org/wiki/Open_Database_Connectivity" target="_blank">ODBC</a>, <a href="http://msdn.microsoft.com/en-us/library/ms188279.aspx" target="_blank">Linked Servers</a>, <a href="http://msdn.microsoft.com/en-us/library/ms188427.aspx" target="_blank">OPENQUERY</a> and the <a href="http://msdn.microsoft.com/en-us/library/ms141752.aspx" target="_blank">Script Task</a>. All of these work well but they&#8217;re just not as convenient as loading files with the <a href="http://msdn.microsoft.com/en-us/library/ms141237.aspx" target="_blank">OLED Destination</a>. </p>
<p>I recently attempted to use the ADO.NET Destination to load files into MySQL without luck. I <a href="http://twitter.com/rhyscampbell/status/12211827588" target="_blank">tweeted</a> about this and Todd McDermid (<a href="http://toddmcdermid.blogspot.com/" target="_blank">blog</a> | <a href="http://twitter.com/Todd_McDermid" target="_blank">twitter</a>) kindly pointed me at a blog post about <a href="http://blogs.msdn.com/mattm/archive/2009/01/07/writing-to-a-mysql-database-from-ssis.aspx" target="_blank">writing data to MySQL with SSIS</a>. This works well but I am having difficulties with data conversions. The author of the blog post, <a href="http://blogs.msdn.com/user/Profile.aspx?UserID=54640" target="_blank">Matt Mason</a>, posted a follow up article with the various options of <a href="http://blogs.msdn.com/mattm/archive/2009/03/02/how-do-i-do-update-and-delete-if-i-don-t-have-an-oledb-provider.aspx" target="_blank">SSIS / MySQL interaction</a>. All of these involve pushing data into MySQL but I though why not get it to pull data. Here&#8217;s an outline of this approach.</p>
<p>First create the following table in a MySQL database. This is a clone of the HumanResources.Employees table from the <a href="http://msdn.microsoft.com/en-us/library/ms124501.aspx" target="_blank">AdventureWorks</a> sample database. We will be doing a simple extract-truncate-load of the table into MySQL from SQL Server.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p745code4'); return false;">View Code</a> MYSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p7454"><td class="code" id="p745code4"><pre class="mysql" style="font-family:monospace;"><span style="color: #808080; font-style: italic;"># Create MySQL replica table of the HumanResources.Employee from the AdventureWorks database</span>
<span style="color: #990099; font-weight: bold;">CREATE</span> <span style="color: #990099; font-weight: bold;">TABLE</span> <span style="color: #008000;">`HumanResourcesEmployee`</span>
<span style="color: #FF00FF;">&#40;</span>
	<span style="color: #008000;">`EmployeeID`</span> <span style="color: #999900; font-weight: bold;">INT</span> <span style="color: #990099; font-weight: bold;">PRIMARY KEY</span> <span style="color: #CC0099; font-weight: bold;">NOT</span> <span style="color: #9900FF; font-weight: bold;">NULL</span><span style="color: #000033;">,</span>
	<span style="color: #008000;">`NationalIDNumber`</span> <span style="color: #999900; font-weight: bold;">VARCHAR</span><span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">15</span><span style="color: #FF00FF;">&#41;</span> <span style="color: #CC0099; font-weight: bold;">NOT</span> <span style="color: #9900FF; font-weight: bold;">NULL</span><span style="color: #000033;">,</span>
	<span style="color: #008000;">`ContactID`</span> <span style="color: #999900; font-weight: bold;">INT</span> <span style="color: #CC0099; font-weight: bold;">NOT</span> <span style="color: #9900FF; font-weight: bold;">NULL</span><span style="color: #000033;">,</span>
	<span style="color: #008000;">`LoginID`</span> <span style="color: #999900; font-weight: bold;">VARCHAR</span><span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">256</span><span style="color: #FF00FF;">&#41;</span> <span style="color: #CC0099; font-weight: bold;">NOT</span> <span style="color: #9900FF; font-weight: bold;">NULL</span><span style="color: #000033;">,</span>
	<span style="color: #008000;">`ManagerID`</span> <span style="color: #999900; font-weight: bold;">INT</span> <span style="color: #9900FF; font-weight: bold;">NULL</span><span style="color: #000033;">,</span>
	<span style="color: #008000;">`Title`</span> <span style="color: #999900; font-weight: bold;">VARCHAR</span><span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">50</span><span style="color: #FF00FF;">&#41;</span> <span style="color: #CC0099; font-weight: bold;">NOT</span> <span style="color: #9900FF; font-weight: bold;">NULL</span><span style="color: #000033;">,</span>
	<span style="color: #008000;">`BirthDate`</span> <span style="color: #999900; font-weight: bold;">DATETIME</span> <span style="color: #CC0099; font-weight: bold;">NOT</span> <span style="color: #9900FF; font-weight: bold;">NULL</span><span style="color: #000033;">,</span>
	<span style="color: #008000;">`MaritalStatus`</span> <span style="color: #000099;">CHAR</span><span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">1</span><span style="color: #FF00FF;">&#41;</span> <span style="color: #CC0099; font-weight: bold;">NOT</span> <span style="color: #9900FF; font-weight: bold;">NULL</span><span style="color: #000033;">,</span>
	<span style="color: #008000;">`Gender`</span> <span style="color: #000099;">CHAR</span><span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">1</span><span style="color: #FF00FF;">&#41;</span> <span style="color: #CC0099; font-weight: bold;">NOT</span> <span style="color: #9900FF; font-weight: bold;">NULL</span><span style="color: #000033;">,</span>
	<span style="color: #008000;">`HireDate`</span> <span style="color: #999900; font-weight: bold;">DATETIME</span> <span style="color: #CC0099; font-weight: bold;">NOT</span> <span style="color: #9900FF; font-weight: bold;">NULL</span><span style="color: #000033;">,</span>
	<span style="color: #008000;">`SalariedFlag`</span> <span style="color: #999900; font-weight: bold;">VARCHAR</span><span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">5</span><span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">,</span>
	<span style="color: #008000;">`VacationHours`</span> <span style="color: #999900; font-weight: bold;">SMALLINT</span><span style="color: #000033;">,</span>
	<span style="color: #008000;">`SickLeaveHours`</span> <span style="color: #999900; font-weight: bold;">SMALLINT</span><span style="color: #000033;">,</span>
	<span style="color: #008000;">`CurrentFlag`</span> <span style="color: #999900; font-weight: bold;">VARCHAR</span><span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">5</span><span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">,</span>
	<span style="color: #008000;">`rowguid`</span> <span style="color: #999900; font-weight: bold;">VARCHAR</span><span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">40</span><span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">,</span>
	<span style="color: #008000;">`ModifiedDate`</span> <span style="color: #999900; font-weight: bold;">DATETIME</span>
<span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">;</span></pre></td></tr></table></div>

<p>Next create a new Integration Services project in <a href="http://msdn.microsoft.com/en-us/library/ms173767.aspx" target="_blank">BIDS</a>. Add a <a href="http://msdn.microsoft.com/en-us/library/ms141122.aspx" target="_blank">Data Flow Task</a> to the project and call it &quot;Extract Employees from SQL Server&quot; </p>
<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/04/data_flow_task.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="data flow task" border="0" alt="data flow task thumb SSIS: Loading files into MySQL" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/04/data_flow_task_thumb.png" width="244" height="93" /></a> </p>
<p>This will export the contents of the HumanResources.Employees table and write it to a flat file. Edit the task and add an OLE Source and configure it as illustrated below. The OLEDB connection manager should point at your <a href="http://www.microsoft.com/sqlserver/2008/en/us/default.aspx" target="_blank">SQL Server</a> instance hosting the AdventureWorks database.</p>
<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/04/oledb_source_employees.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="oledb source employees" border="0" alt="oledb source employees thumb SSIS: Loading files into MySQL" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/04/oledb_source_employees_thumb.png" width="560" height="484" /></a> </p>
<p>Next add a <a href="http://msdn.microsoft.com/en-us/library/ms141668.aspx" target="_blank">Flat File Destination</a> and name it &quot;Write Employees File&quot; then connect the OLEDB Source to it. Configure the connection manager as illustrated below. The important things to note here are the fact I&#8217;m writing the file to <strong>E:\Employees.txt</strong> and it is pipe delimited. You may need to alter these according to your setup.</p>
<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/04/flat_file_connection_manager.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="flat file connection manager" border="0" alt="flat file connection manager thumb SSIS: Loading files into MySQL" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/04/flat_file_connection_manager_thumb.png" width="536" height="484" /></a> </p>
<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/04/flat_file_connection_manager_delimiter.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="flat file connection manager delimiter" border="0" alt="flat file connection manager delimiter thumb SSIS: Loading files into MySQL" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/04/flat_file_connection_manager_delimiter_thumb.png" width="536" height="484" /></a> </p>
<p>The dataflow should now look something like below.</p>
<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/04/final_data_flow.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="final data flow" border="0" alt="final data flow thumb SSIS: Loading files into MySQL" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/04/final_data_flow_thumb.png" width="219" height="244" /></a> </p>
<p>Go back to the Control Flow pane and add an <a href="http://technet.microsoft.com/en-us/library/ms141003.aspx" target="_blank">Execute SQL Task</a> to the canvas. Call this task &quot;TRUNCATE TABLE HumanResourcesEmployee&quot;. Edit the task and configure it like below. You need to add a ADO.NET connection manager which references an <a href="http://dev.mysql.com/doc/refman/4.1/en/connector-odbc-configuration-dsn-windows.html" target="_blank">ODBC connection to your MySQL database</a>. You&#8217;ll need the <a href="http://dev.mysql.com/downloads/connector/odbc/5.1.html" target="_blank">MySQL ODBC Driver</a> for this.</p>
<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/04/execute_sql_task_employees.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="execute sql task employees" border="0" alt="execute sql task employees thumb SSIS: Loading files into MySQL" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/04/execute_sql_task_employees_thumb.png" width="570" height="484" /></a> </p>
<p>My ODBC connection looks like below.</p>
<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/04/mysql_odbc_dsn_connection.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="mysql odbc dsn connection" border="0" alt="mysql odbc dsn connection thumb SSIS: Loading files into MySQL" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/04/mysql_odbc_dsn_connection_thumb.png" width="607" height="484" /></a> </p>
<p>Add another Execute SQL Task and call it &quot;Load new Employees file into MySQL&quot;. Connect up the tasks in sequence.</p>
<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/04/data_flow_ssis_mysql.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="data flow ssis mysql" border="0" alt="data flow ssis mysql thumb SSIS: Loading files into MySQL" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/04/data_flow_ssis_mysql_thumb.png" width="244" height="229" /></a> </p>
<p>Edit &quot;Load new Employees file into MySQL&quot;, add the ADO.NET connection to MySQL and enter the below SQL. You will need to change the file path and delimiter character if you have changed them.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p745code5'); return false;">View Code</a> MYSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p7455"><td class="code" id="p745code5"><pre class="mysql" style="font-family:monospace;"><span style="color: #990099; font-weight: bold;">LOAD</span> <span style="color: #990099; font-weight: bold;">DATA</span> <span style="color: #990099; font-weight: bold;">LOCAL</span> <span style="color: #990099; font-weight: bold;">INFILE</span> <span style="color: #008000;">'E:<span style="color: #004000; font-weight: bold;">\\</span>Employees.txt'</span> <span style="color: #808080; font-style: italic;"># Column order MUST match the table </span>
<span style="color: #990099; font-weight: bold;">INTO</span> <span style="color: #990099; font-weight: bold;">TABLE</span> HumanResourcesEmployee
<span style="color: #990099; font-weight: bold;">FIELDS</span> <span style="color: #990099; font-weight: bold;">TERMINATED BY</span> <span style="color: #008000;">'|'</span>
<span style="color: #990099; font-weight: bold;">LINES</span> <span style="color: #990099; font-weight: bold;">TERMINATED BY</span> <span style="color: #008000;">'<span style="color: #004000; font-weight: bold;">\r</span><span style="color: #004000; font-weight: bold;">\n</span>'</span> <span style="color: #808080; font-style: italic;"># Windows line terminator</span>
<span style="color: #990099; font-weight: bold;">IGNORE</span> <span style="color: #008080;">1</span> <span style="color: #990099; font-weight: bold;">LINES</span><span style="color: #000033;">;</span> <span style="color: #808080; font-style: italic;"># Ignore column header line</span></pre></td></tr></table></div>

<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/04/execute_sql_task_employees_mysql.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="execute sql task employees mysql" border="0" alt="execute sql task employees mysql thumb SSIS: Loading files into MySQL" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/04/execute_sql_task_employees_mysql_thumb.png" width="570" height="484" /></a> </p>
<p>I originally attempted to enclose this in a store procedure as this provides more flexibility but MySQL gave the following error.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p745code6'); return false;">View Code</a> MYSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p7456"><td class="code" id="p745code6"><pre class="mysql" style="font-family:monospace;"><span style="color: #990099; font-weight: bold;">LOAD</span> <span style="color: #990099; font-weight: bold;">DATA</span> <span style="color: #CC0099; font-weight: bold;">is not</span> allowed <span style="color: #990099; font-weight: bold;">in</span> stored procedures</pre></td></tr></table></div>

<p>MySQL prevents you from using <a href="http://dev.mysql.com/doc/refman/5.1/en/load-data.html" target="_blank">LOAD DATA INFILE</a> inside stored procedures (my major beef with MySQL is what you can and cannot do inside stored procedures) so we have no choice but to enter it in the SQLStatement pane. Execute the package and if all goes well the Employees.txt file will be loaded into MySQL.</p>
<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/04/execute_ssis_mysql_load.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="execute_ssis_mysql_load" border="0" alt="execute ssis mysql load thumb SSIS: Loading files into MySQL" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/04/execute_ssis_mysql_load_thumb.png" width="244" height="227" /></a> </p>
<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/04/employees_loaded_into_mysql_ssis.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="employees loaded into mysql ssis" border="0" alt="employees loaded into mysql ssis thumb SSIS: Loading files into MySQL" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/04/employees_loaded_into_mysql_ssis_thumb.png" width="644" height="247" /></a> </p>
<p>I&#8217;ve not yet used this in production so use with caution, but it&#8217;s simple to setup and fast. One thing to note for this load is that MySQL has replaced backslashes with hyphens in the <strong>LoginId</strong> column so it would be sensible to check all data conversions.</p>
<p><map name='google_ad_map_745_a45beff5d2e172f6'>
<area shape='rect' href='http://imageads.googleadservices.com/pagead/imgclick/745?pos=0' coords='1,2,367,28' />
<area shape='rect' href='http://services.google.com/feedback/abg' coords='384,10,453,23'/></map>
<img usemap='#google_ad_map_745_a45beff5d2e172f6' border='0' src='http://imageads.googleadservices.com/pagead/ads?format=468x30_aff_img&amp;client=&amp;channel=&amp;output=png&amp;cuid=745&amp;url=http%3A%2F%2Fwww.youdidwhatwithtsql.com%2Fssis-loading-files-into-mysql%2F745' title="SSIS: Loading files into MySQL" alt=" SSIS: Loading files into MySQL" /></p><p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/ssis-loading-files-into-mysql/745">SSIS: Loading files into MySQL</a></p>
<div class="none"><div class="g-plusone" data-href="http://www.youdidwhatwithtsql.com/ssis-loading-files-into-mysql/745" size="standard" count="true"></div></div>]]></content:encoded>
			<wfw:commentRss>http://www.youdidwhatwithtsql.com/ssis-loading-files-into-mysql/745/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>SSIS Execute Process Task not registered for use on this Computer</title>
		<link>http://www.youdidwhatwithtsql.com/ssis-execute-process-task-not-registered-for-use-on-this-computer/716</link>
		<comments>http://www.youdidwhatwithtsql.com/ssis-execute-process-task-not-registered-for-use-on-this-computer/716#comments</comments>
		<pubDate>Sat, 10 Apr 2010 10:57:11 +0000</pubDate>
		<dc:creator>Rhys</dc:creator>
				<category><![CDATA[SSIS]]></category>
		<category><![CDATA[Execute Process Task]]></category>

		<guid isPermaLink="false">http://www.youdidwhatwithtsql.com/ssis-execute-process-task-not-registered-for-use-on-this-computer/716</guid>
		<description><![CDATA[Whilst doing some maintenance on an SSIS package I encountered the below error when attempting to edit an Execute Process Task.<p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/ssis-execute-process-task-not-registered-for-use-on-this-computer/716">SSIS Execute Process Task not registered for use on this Computer</a></p>
]]></description>
			<content:encoded><![CDATA[<p>Whilst doing some maintenance on an <a href="http://www.microsoft.com/sqlserver/2005/en/us/integration-services.aspx" target="_blank">SSIS</a> package I encountered the below error when attempting to edit an <a href="http://msdn.microsoft.com/en-us/library/ms141166.aspx" target="_blank">Execute Process Task</a>.</p>
<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/04/ssis_task_not_registered.png"><img style="display: inline; border-width: 0px;" title="The task with the name &quot;Tectia - Download Files&quot; and the creation name &quot;Microsoft.SqlServer.Dts.Tasks.ExecuteProcess.ExecuteProcess, Microsoft.SqlServer.ExecProcTask, Version=9.0.242.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91&quot; is not registered for use on this computer" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/04/ssis_task_not_registered_thumb.png" border="0" alt="ssis task not registered thumb SSIS Execute Process Task not registered for use on this Computer" Version=9.0.242.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91&quot; is not registered for use on this computer" width="644" height="188" /></a></p>
<p>The package executes daily so I&#8217;m lucky I caught this in time. I Googled around and found <a href="http://social.msdn.microsoft.com/Forums/en-US/sqlintegrationservices/thread/ebdae67c-2b63-4e8f-9c8b-35fb6fd96ecf" target="_blank">this thread</a> which advised a reinstall of SSIS. Unfortunately I didn&#8217;t have this luxury so I decided to do some digging. I found the location of the <a href="http://msdn.microsoft.com/en-us/library/ms141166.aspx" target="_blank">Execute Process Task</a> dll file in the Choose Toolbox Items dialog.</p>
<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/04/Execute_Process_Task.png"><img style="display: inline; border-width: 0px;" title="Execute Process Task" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/04/Execute_Process_Task_thumb.png" border="0" alt="Execute Process Task thumb SSIS Execute Process Task not registered for use on this Computer" width="644" height="468" /></a></p>
<p>Since the error was complaining about the task being unregistered I thought I&#8217;d try registering the dll in the <a href="http://en.wikipedia.org/wiki/Global_Assembly_Cache" target="_blank">GAC</a>. First I executed the below command;</p>
<pre>C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322&gt;gacutil /i "C:\Program Files\Micro
oft SQL Server\90\dts\tasks\Microsoft.SqlServer.ExecProcTask.dll"

Microsoft (R) .NET Global Assembly Cache Utility.  Version 1.1.4322.573
Copyright (C) Microsoft Corporation 1998-2002. All rights reserved.

Failure adding assembly to the cache: Unknown Error

C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322&gt;</pre>
<p>This failed because I was using a different version of the <a href="http://msdn.microsoft.com/en-us/library/ex0ss12c(VS.80).aspx" target="_blank">gacutil</a> tool. As it turns out you have to use the appropriate version of gacutil for the dll. A quick 350MB download of the <a href="http://www.microsoft.com/downloads/details.aspx?FamilyID=fe6f2099-b7b4-4f47-a244-c96d69c35dec&amp;displaylang=en" target="_blank">.NET Framework 2.0 Software Development Kit</a> and I was ready to roll. I reattempted the above command with the correct version.</p>
<pre>C:\Program Files\Microsoft Visual Studio 8\SDK\v2.0\Bin&gt;gacutil /i "C:\Program F
iles\Microsoft SQL Server\90\dts\tasks\Microsoft.SqlServer.ExecProcTask.dll"
Microsoft (R) .NET Global Assembly Cache Utility.  Version 2.0.50727.42
Copyright (c) Microsoft Corporation.  All rights reserved.

Assembly successfully added to the cache

C:\Program Files\Microsoft Visual Studio 8\SDK\v2.0\Bin&gt;</pre>
<p>After this was registered I was able to edit the task with no problems. I couldn&#8217;t find any issues with any other task types so I was happy I didn&#8217;t go down the reinstall route.</p>
<p><map name='google_ad_map_716_a45beff5d2e172f6'>
<area shape='rect' href='http://imageads.googleadservices.com/pagead/imgclick/716?pos=0' coords='1,2,367,28' />
<area shape='rect' href='http://services.google.com/feedback/abg' coords='384,10,453,23'/></map>
<img usemap='#google_ad_map_716_a45beff5d2e172f6' border='0' src='http://imageads.googleadservices.com/pagead/ads?format=468x30_aff_img&amp;client=&amp;channel=&amp;output=png&amp;cuid=716&amp;url=http%3A%2F%2Fwww.youdidwhatwithtsql.com%2Fssis-execute-process-task-not-registered-for-use-on-this-computer%2F716' title="SSIS Execute Process Task not registered for use on this Computer" alt=" SSIS Execute Process Task not registered for use on this Computer" /></p><p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/ssis-execute-process-task-not-registered-for-use-on-this-computer/716">SSIS Execute Process Task not registered for use on this Computer</a></p>
<div class="none"><div class="g-plusone" data-href="http://www.youdidwhatwithtsql.com/ssis-execute-process-task-not-registered-for-use-on-this-computer/716" size="standard" count="true"></div></div>]]></content:encoded>
			<wfw:commentRss>http://www.youdidwhatwithtsql.com/ssis-execute-process-task-not-registered-for-use-on-this-computer/716/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>SSIS: Make your output files dynamic part 2</title>
		<link>http://www.youdidwhatwithtsql.com/ssis-make-your-output-file-dynamic-part-2/664</link>
		<comments>http://www.youdidwhatwithtsql.com/ssis-make-your-output-file-dynamic-part-2/664#comments</comments>
		<pubDate>Sat, 20 Feb 2010 19:55:10 +0000</pubDate>
		<dc:creator>Rhys</dc:creator>
				<category><![CDATA[SSIS]]></category>

		<guid isPermaLink="false">http://www.youdidwhatwithtsql.com/ssis-make-your-output-file-dynamic-part-2/664</guid>
		<description><![CDATA[A few weeks ago I blogged about my attempts to make dynamic output files in ssis. The idea here was to make an ssis package, producing a text file output, that would cope with complete changes to the data source. If you wanted to add a column all you needed to do was change the [...]<p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/ssis-make-your-output-file-dynamic-part-2/664">SSIS: Make your output files dynamic part 2</a></p>
]]></description>
			<content:encoded><![CDATA[<p>A few weeks ago I blogged about my attempts to make <a href="http://www.youdidwhatwithtsql.com/ssis-make-your-output-files-dynamic/616" target="_blank">dynamic output files in ssis</a>. The idea here was to make an <a href="http://www.microsoft.com/sqlserver/2005/en/us/integration-services.aspx" target="_blank">ssis</a> package, producing a text file output, that would cope with complete changes to the data source. If you wanted to add a column all you needed to do was change the stored procedure definition that the data was derived from. While this was functional it did have a couple of issues.</p>
<ul>
<li>The Script Task seems to buckle for XML data types giving an unsupported conversion error. (Use the Person.Contact table in the AdventureWorks database to view this issue). </li>
<li>The package does not seem to cope with a large number of rows. Execution appears to freeze for datasets containing somewhat over 1,000 rows. (Use the Person.Address table in the AdventureWorks database to view this issue). I’m wondering if this is due to size limitation with the System.Object type. </li>
</ul>
<p>Here&#8217;s quick run through of how I resolved the issues. The basic idea was to do all of the data access in VB.Net rather than using an <a href="http://technet.microsoft.com/en-us/library/ms141003.aspx" target="_blank">Execute SQL Task</a> to store a resultset in a System.Object variable.</p>
<p>Open <a href="http://msdn.microsoft.com/en-us/library/ms173767.aspx" target="_blank">BIDS</a> and create a new Integration Services Project.</p>
<p>First add a string variable to the project as illustrated below.</p>
<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/02/ssis_filename_variable.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="ssis filename variable" border="0" alt="ssis filename variable thumb SSIS: Make your output files dynamic part 2" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/02/ssis_filename_variable_thumb.png" width="644" height="156" /></a> </p>
<p>Set the <a href="http://msdn.microsoft.com/en-us/library/microsoft.sqlserver.dts.runtime.variable.evaluateasexpression.aspx" target="_blank">EvaluateAsExpression</a> property to true and enter the following expression.</p>
<pre> (DT_WSTR,4)YEAR(GETDATE()) + "_"
    + RIGHT("0" + (DT_WSTR,2)MONTH(GETDATE()), 2) + "_"
    + RIGHT("0" + (DT_WSTR,2)DAY( GETDATE()), 2) + "_"
    + RIGHT("0" + (DT_WSTR,2)DATEPART("hh", GETDATE()), 2) + "_" + RIGHT("0" + (DT_WSTR,2)DATEPART("mi", GETDATE()), 2) + "_" + RIGHT("0" + (DT_WSTR,2)DATEPART("ss", GETDATE()), 2) + ".txt"</pre>
<p>This will provide us with a datetime stamped filename looking similar to <strong>2010_02_20_15_40_36.txt</strong>.</p>
<p>Next add another string variable called <strong>query</strong> and enter a statement to execute a stored procedure that returns a resultset. Any one will do as the package should be agnostic to the results. </p>
<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/02/ssis_query_variable.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="ssis query variable" border="0" alt="ssis query variable thumb SSIS: Make your output files dynamic part 2" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/02/ssis_query_variable_thumb.png" width="644" height="159" /></a> </p>
<p>I&#8217;m using the below stored procedure running against the <a href="http://msdn.microsoft.com/en-us/library/ms124501.aspx" target="_blank">AdventureWorks</a> sample database.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p664code9'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p6649"><td class="code" id="p664code9"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">USE</span> <span style="color: #808080;">&#91;</span>AdventureWorks<span style="color: #808080;">&#93;</span>
GO
<span style="color: #008080;">/****** Object:  StoredProcedure [dbo].[usp_test]    Script Date: 02/20/2010 15:48:49 ******/</span>
<span style="color: #0000FF;">SET</span> ANSI_<span style="color: #808080;">NULL</span>S <span style="color: #0000FF;">ON</span>
GO
<span style="color: #0000FF;">SET</span> QUOTED_IDENTIFIER <span style="color: #0000FF;">ON</span>
GO
<span style="color: #008080;">-- =============================================</span>
<span style="color: #008080;">-- Author:		Rhys Campbell</span>
<span style="color: #008080;">-- Create date: 2010-02-20</span>
<span style="color: #008080;">-- Description:	Test proc</span>
<span style="color: #008080;">-- =============================================</span>
<span style="color: #0000FF;">ALTER</span> <span style="color: #0000FF;">PROCEDURE</span> <span style="color: #808080;">&#91;</span>dbo<span style="color: #808080;">&#93;</span>.<span style="color: #808080;">&#91;</span>usp_test<span style="color: #808080;">&#93;</span>
<span style="color: #0000FF;">AS</span>
<span style="color: #0000FF;">BEGIN</span>
	<span style="color: #008080;">-- SET NOCOUNT ON added to prevent extra result sets from</span>
	<span style="color: #008080;">-- interfering with SELECT statements.</span>
	<span style="color: #0000FF;">SET</span> <span style="color: #0000FF;">NOCOUNT</span> <span style="color: #0000FF;">ON</span>;
&nbsp;
    	<span style="color: #008080;">-- Insert statements for procedure here</span>
	<span style="color: #0000FF;">SELECT</span> <span style="color: #808080;">*</span>
	<span style="color: #0000FF;">FROM</span> Person.<span style="color: #202020;">Contact</span>;
&nbsp;
<span style="color: #0000FF;">END</span></pre></td></tr></table></div>

<p>Next add an <a href="http://msdn.microsoft.com/en-us/library/ms141676.aspx" target="_blank">ADO.NET Connection Manager</a> to the project pointing this at the database containing your stored procedure. Rename the connection manager to <strong>DBConnection</strong> as we will be referencing this by name in code later.</p>
<p>Drop a Script Task onto the designer and edit its properties. Add the <strong>filename</strong> and <strong>query </strong>variables to the <a href="http://msdn.microsoft.com/en-us/library/microsoft.sqlserver.dts.pipeline.scriptcomponent.readonlyvariables.aspx" target="_blank">ReadOnlyVariables</a> property. </p>
<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/02/ssis_script_task_properties.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="ssis script_task properties" border="0" alt="ssis script task properties thumb SSIS: Make your output files dynamic part 2" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/02/ssis_script_task_properties_thumb.png" width="570" height="484" /></a> </p>
<p>Set the script language to <strong>Microsoft Visual Basic 2008</strong> and then click &quot;Edit Script&quot;. Paste the below code into the editor.</p>
<pre lang="VB.NET">
Imports System
Imports System.Data
Imports System.Math
Imports Microsoft.SqlServer.Dts.Runtime

<System.AddIn.AddIn("ScriptMain", Version:="1.0", Publisher:="", Description:="")> _
<System.CLSCompliantAttribute(False)> _
Partial Public Class ScriptMain
	Inherits Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase

	Enum ScriptResults
		Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success
		Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure
	End Enum

    Public Sub Main()

        ' Get the datetime stamped filename
        Dim filename As String = Dts.Variables("filename").Value.ToString
        ' Get the user desktop directory. We'll write the output file to here
        ' You probably want to use some other location for production purposes
        Dim userProfileDir As String = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory).ToString

        ' Get the Connection string. Must be called "DBConnection"
        Dim connStr As String = Me.Dts.Connections.Item("DBConnection").ConnectionString.ToString

        ' Get the query set in the ssis variables
        Dim query As String = Dts.Variables("query").Value.ToString

        ' Setup DB access stuff
        Dim con As SqlClient.SqlConnection = New SqlClient.SqlConnection(connStr)
        Dim comm As SqlClient.SqlCommand = New SqlClient.SqlCommand(query, con)
        Dim reader As SqlClient.SqlDataReader

        ' Open the connection
        con.Open()

        ' Get results
        reader = comm.ExecuteReader()

        ' temp variable to hold lines
        Dim line As String = ""

        ' Get the column names from the resultset
        For index As Integer = 0 To (reader.FieldCount - 1)
            line &#038;= reader.GetName(index) &#038; "|"
        Next

        ' remove last pipe
        line = line.Substring(0, line.Length - 1)

        ' write the column headers to the text file
        appendToTextFile(userProfileDir &#038; "\" &#038; filename, line)

        ' Write each row in the resultset to the text file
        While reader.Read()

            line = ""
            ' Add each column value to line
            For index As Integer = 0 To (reader.FieldCount - 1)
                ' Removing any pipes and newline to avoid screwing up our file
                line &#038;= reader.GetValue(index).ToString.Replace("|", "").Replace(ControlChars.CrLf, vbNullString) &#038; "|"
            Next

            ' remove last pipe and add a new line
            line = line.Substring(0, line.Length - 1)

            ' write the line to the output file
            appendToTextFile(userProfileDir &#038; "\" &#038; filename, line)

        End While

        ' clean up
        con.Close()
        comm = Nothing
        reader = Nothing
        con = Nothing

        Dts.TaskResult = ScriptResults.Success

    End Sub
    Public Sub appendToTextFile(ByVal file As String, ByVal line As String)

        ' Create a writer object that appends to a text file if it exists
        Dim objWriter As New System.IO.StreamWriter(file, True)
        objWriter.WriteLine(line)
        objWriter.Close()
        objWriter = Nothing

    End Sub

End Class</pre>
<p>Now execute the package and check your desktop for the output file. My output file looked like below.</p>
<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/02/ssis_output_file.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="ssis output file" border="0" alt="ssis output file thumb SSIS: Make your output files dynamic part 2" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/02/ssis_output_file_thumb.png" width="644" height="481" /></a> </p>
<p>Now alter your stored procedure so it returns a different resultset. Here&#8217;s another one I used in the AdventureWorks database.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p664code10'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p66410"><td class="code" id="p664code10"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">USE</span> <span style="color: #808080;">&#91;</span>AdventureWorks<span style="color: #808080;">&#93;</span>
GO
<span style="color: #008080;">/****** Object:  StoredProcedure [dbo].[usp_test]    Script Date: 02/20/2010 19:20:55 ******/</span>
<span style="color: #0000FF;">SET</span> ANSI_<span style="color: #808080;">NULL</span>S <span style="color: #0000FF;">ON</span>
GO
<span style="color: #0000FF;">SET</span> QUOTED_IDENTIFIER <span style="color: #0000FF;">ON</span>
GO
<span style="color: #008080;">-- =============================================</span>
<span style="color: #008080;">-- Author:		Rhys Campbell</span>
<span style="color: #008080;">-- Create date: 2010-02-20</span>
<span style="color: #008080;">-- Description:	Test proc</span>
<span style="color: #008080;">-- =============================================</span>
<span style="color: #0000FF;">ALTER</span> <span style="color: #0000FF;">PROCEDURE</span> <span style="color: #808080;">&#91;</span>dbo<span style="color: #808080;">&#93;</span>.<span style="color: #808080;">&#91;</span>usp_test<span style="color: #808080;">&#93;</span>
<span style="color: #0000FF;">AS</span>
<span style="color: #0000FF;">BEGIN</span>
	<span style="color: #008080;">-- SET NOCOUNT ON added to prevent extra result sets from</span>
	<span style="color: #008080;">-- interfering with SELECT statements.</span>
	<span style="color: #0000FF;">SET</span> <span style="color: #0000FF;">NOCOUNT</span> <span style="color: #0000FF;">ON</span>;
&nbsp;
    <span style="color: #008080;">-- Insert statements for procedure here</span>
	<span style="color: #0000FF;">SELECT</span> <span style="color: #808080;">*</span>
	<span style="color: #0000FF;">FROM</span> Person.<span style="color: #202020;">Address</span>;
&nbsp;
<span style="color: #0000FF;">END</span></pre></td></tr></table></div>

<p>Run the package again and you should find another output file on your desktop.</p>
<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/02/ssis_output_file_2.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="ssis output file 2" border="0" alt="ssis output file 2 thumb SSIS: Make your output files dynamic part 2" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/02/ssis_output_file_2_thumb.png" width="644" height="481" /></a> </p>
<p>I&#8217;ve not thoroughly tested this code but it appears my initial issues have been resolved. This method does take significantly longer to write the output file than the traditional data flow method. This shouldn&#8217;t be a huge issue for the purposes I&#8217;m thinking of using it in so I should be soon deploying this into a production environment.</p>
<p><map name='google_ad_map_664_a45beff5d2e172f6'>
<area shape='rect' href='http://imageads.googleadservices.com/pagead/imgclick/664?pos=0' coords='1,2,367,28' />
<area shape='rect' href='http://services.google.com/feedback/abg' coords='384,10,453,23'/></map>
<img usemap='#google_ad_map_664_a45beff5d2e172f6' border='0' src='http://imageads.googleadservices.com/pagead/ads?format=468x30_aff_img&amp;client=&amp;channel=&amp;output=png&amp;cuid=664&amp;url=http%3A%2F%2Fwww.youdidwhatwithtsql.com%2Fssis-make-your-output-file-dynamic-part-2%2F664' title="SSIS: Make your output files dynamic part 2" alt=" SSIS: Make your output files dynamic part 2" /></p><p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/ssis-make-your-output-file-dynamic-part-2/664">SSIS: Make your output files dynamic part 2</a></p>
<div class="none"><div class="g-plusone" data-href="http://www.youdidwhatwithtsql.com/ssis-make-your-output-file-dynamic-part-2/664" size="standard" count="true"></div></div>]]></content:encoded>
			<wfw:commentRss>http://www.youdidwhatwithtsql.com/ssis-make-your-output-file-dynamic-part-2/664/feed</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>SSIS: Make your output files dynamic</title>
		<link>http://www.youdidwhatwithtsql.com/ssis-make-your-output-files-dynamic/616</link>
		<comments>http://www.youdidwhatwithtsql.com/ssis-make-your-output-files-dynamic/616#comments</comments>
		<pubDate>Sat, 06 Feb 2010 15:35:09 +0000</pubDate>
		<dc:creator>Rhys</dc:creator>
				<category><![CDATA[SSIS]]></category>

		<guid isPermaLink="false">http://www.youdidwhatwithtsql.com/ssis-make-your-output-files-dynamic/616</guid>
		<description><![CDATA[I like making my SSIS packages as dynamic as possible. Once that package has been deployed into production I want to avoid opening it up in BIDS if possible. I&#8217;ve blogged previously about using Stored Procedures in Execute SQL Tasks but this only gives us flexibility in terms of the where clause. We have no [...]<p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/ssis-make-your-output-files-dynamic/616">SSIS: Make your output files dynamic</a></p>
]]></description>
			<content:encoded><![CDATA[<p>I like making my <a href="http://www.microsoft.com/sqlserver/2005/en/us/integration-services.aspx" target="_blank">SSIS</a> packages as dynamic as possible. Once that package has been deployed into production I want to avoid opening it up in <a href="http://msdn.microsoft.com/en-us/library/ms173767.aspx" target="_blank">BIDS</a> if possible. I&#8217;ve blogged previously about using <a href="http://www.youdidwhatwithtsql.com/execute-sq-procedure-task/559" target="_blank">Stored Procedures in Execute SQL Tasks</a> but this only gives us flexibility in terms of the where clause. We have no flexibility in terms of the columns unless we open the package up in BIDS.</p>
<p>I wanted to build a little more flexibility into my packages. Wouldn&#8217;t it be great if we could simply modify a stored procedure to include additional columns, and these changes would be reflected in the output files, with no further work required? Here&#8217;s an outline of my first steps towards achieving this.</p>
<ul>
<li>First create a stored procedure, that returns a resultset, in a test database. Anyone will do. Here&#8217;s one I created in the <a href="http://msdn.microsoft.com/en-us/library/ms124501.aspx" target="_blank">AdventureWorks</a> test database.</li>
</ul>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p616code14'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p61614"><td class="code" id="p616code14"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">USE</span> <span style="color: #808080;">&#91;</span>AdventureWorks<span style="color: #808080;">&#93;</span>
GO
<span style="color: #0000FF;">SET</span> ANSI_<span style="color: #808080;">NULL</span>S <span style="color: #0000FF;">ON</span>
GO
<span style="color: #0000FF;">SET</span> QUOTED_IDENTIFIER <span style="color: #0000FF;">ON</span>
GO
<span style="color: #0000FF;">CREATE</span> <span style="color: #0000FF;">PROCEDURE</span> <span style="color: #808080;">&#91;</span>dbo<span style="color: #808080;">&#93;</span>.<span style="color: #808080;">&#91;</span>usp_test<span style="color: #808080;">&#93;</span> <span style="color: #0000FF;">AS</span>
<span style="color: #0000FF;">BEGIN</span>
&nbsp;
	<span style="color: #0000FF;">SELECT</span> <span style="color: #808080;">*</span>
	<span style="color: #0000FF;">FROM</span> Production.<span style="color: #202020;">Product</span>;
&nbsp;
<span style="color: #0000FF;">END</span></pre></td></tr></table></div>

<ul>
<li>Open up <a href="http://msdn.microsoft.com/en-us/library/ms173767.aspx" target="_blank">BIDS</a> and create a new integration services project.</li>
<li>Add an object variable like below called <strong>resultset</strong>.</li>
</ul>
<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/02/ssis_resultset_variable.png"><img style="display: inline; border-width: 0px;" title="ssis resultset variable" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/02/ssis_resultset_variable_thumb.png" border="0" alt="ssis resultset variable thumb SSIS: Make your output files dynamic" width="644" height="150" /></a></p>
<ul>
<li>Add an <a href="http://technet.microsoft.com/en-us/library/ms141003.aspx" target="_blank">Execute SQL Task</a> to the designer and configure it to execute the<strong> </strong>procedure you created earlier. Configure it similarly to below, ensuring <strong>Result Set </strong>is set to <strong>Full result set</strong>.</li>
</ul>
<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/02/ssis_exec_sql_task.png"><img style="display: inline; border-width: 0px;" title="ssis exececute sql task" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/02/ssis_exec_sql_task_thumb.png" border="0" alt="ssis exec sql task thumb SSIS: Make your output files dynamic" width="570" height="484" /></a></p>
<ul>
<li>Go to the <strong>Result Set</strong> tab and add a mapping to the <strong>resultset </strong>object variable. Configure this exactly as shown below.</li>
</ul>
<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/02/ssis_exec_sql_task_resultset.png"><img style="display: inline; border-width: 0px;" title="ssis_exec_sql_task_resultset" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/02/ssis_exec_sql_task_resultset_thumb.png" border="0" alt="ssis exec sql task resultset thumb SSIS: Make your output files dynamic" width="570" height="484" /></a></p>
<ul>
<li>Add a <a href="http://msdn.microsoft.com/en-us/library/ms141752.aspx" target="_blank">Script Task</a> to the designer and connect the Execute SQL Task to it. Edit the Script Task and enter the <strong>resultset </strong>variable in the <strong>ReadOnlyVariable </strong>box.</li>
</ul>
<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/02/ssis_script_task.png"><img style="display: inline; border-width: 0px;" title="ssis script task" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/02/ssis_script_task_thumb.png" border="0" alt="ssis script task thumb SSIS: Make your output files dynamic" width="570" height="484" /></a></p>
<ul>
<li>Click &#8220;Edit Script&#8221; and paste the below code into <a href="http://blogs.msdn.com/vsta/" target="_blank">VSTA</a>. The only part of this code you should need to change is the path where the text file is written to.</li>
</ul>
<pre lang="Visual BASIC">' Microsoft SQL Server Integration Services Script Task
' Write scripts using Microsoft Visual Basic 2008.
' The ScriptMain is the entry point class of the script.

Imports System
Imports System.Data
Imports System.Math
Imports Microsoft.SqlServer.Dts.Runtime
Imports System.Data.OleDb
Imports System.IO

 _
 _
Partial Public Class ScriptMain
    Inherits Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase

    Enum ScriptResults
        Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success
        Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure
    End Enum

    Public Sub Main()
        '
        ' Add your code here
        '
        Dim filename As String = Format(Date.Now, "yyyy-MM-dd_hh_mm_ss") &amp; ".csv"
        Dim fileContents As String = ""

        Dim oledb As OleDbDataAdapter = New OleDbDataAdapter()
        Dim table As DataTable = New DataTable()
        Dim rs As System.Object = Dts.Variables("resultset").Value

        oledb.Fill(table, rs)

        ' Get the column names
        For Each col In table.Columns
            fileContents &amp;= col.ColumnName &amp; "|"
        Next

        ' remove last pipe
        fileContents = fileContents.Substring(0, fileContents.Length - 1)
        fileContents &amp;= Environment.NewLine

        ' For each row in the dataset
        Dim i As Integer
        For Each row As DataRow In table.Rows
            ' For each column in the row
            For i = 1 To table.Columns.Count
                fileContents &amp;= row(i - 1).ToString() &amp; "|"
            Next

            ' Remove last pipe and add a newline to the end of each row
            fileContents = fileContents.Substring(0, fileContents.Length - 1)
            fileContents &amp;= Environment.NewLine

        Next

        ' write data to the text file
        ' Change the path to something appropriate
        SaveTextToFile(fileContents, "C:\Users\Rhys\Desktop\" &amp; filename)

        Dts.TaskResult = ScriptResults.Success
    End Sub

    ' Function from http://www.freevbcode.com/ShowCode.Asp?ID=4492
    Public Function SaveTextToFile(ByVal strData As String, _
     ByVal FullPath As String, _
       Optional ByVal ErrInfo As String = "") As Boolean

        Dim bAns As Boolean = False
        Dim objReader As StreamWriter
        Try

            objReader = New StreamWriter(FullPath)
            objReader.Write(strData)
            objReader.Close()
            bAns = True
        Catch Ex As Exception
            ErrInfo = Ex.Message

        End Try
        Return bAns
    End Function

End Class</pre>
<ul>
<li>Save the script and return to the designer. The final package should look something like below.</li>
</ul>
<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/02/ssis_dynamic_output_file_package.png"><img style="display: inline; border-width: 0px;" title="ssis dynamic output file package" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/02/ssis_dynamic_output_file_package_thumb.png" border="0" alt="ssis dynamic output file package thumb SSIS: Make your output files dynamic" width="240" height="214" /></a></p>
<ul>
<li>Execute the package and then check the location where your output file should be written to. Here&#8217;s what the file should look like if you&#8217;re using my <strong>usp_test </strong>stored procedure in the AdventureWorks database.</li>
</ul>
<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/02/AdventureWorks_Output_File_1.png"><img style="display: inline; border-width: 0px;" title="AdventureWorks Output File Production.Products" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/02/AdventureWorks_Output_File_1_thumb.png" border="0" alt="AdventureWorks Output File 1 thumb SSIS: Make your output files dynamic" width="244" height="176" /></a></p>
<ul>
<li>Now comes the fun part! Modify the <strong>usp_test</strong> procedure to select from a completely different table. For example;</li>
</ul>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p616code15'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p61615"><td class="code" id="p616code15"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">USE</span> <span style="color: #808080;">&#91;</span>AdventureWorks<span style="color: #808080;">&#93;</span>
GO
<span style="color: #0000FF;">SET</span> ANSI_<span style="color: #808080;">NULL</span>S <span style="color: #0000FF;">ON</span>
GO
<span style="color: #0000FF;">SET</span> QUOTED_IDENTIFIER <span style="color: #0000FF;">ON</span>
GO
<span style="color: #0000FF;">ALTER</span> <span style="color: #0000FF;">PROCEDURE</span> <span style="color: #808080;">&#91;</span>dbo<span style="color: #808080;">&#93;</span>.<span style="color: #808080;">&#91;</span>usp_test<span style="color: #808080;">&#93;</span> <span style="color: #0000FF;">AS</span>
<span style="color: #0000FF;">BEGIN</span>
&nbsp;
	<span style="color: #0000FF;">SELECT</span> <span style="color: #808080;">*</span>
	<span style="color: #0000FF;">FROM</span> HumanResources.<span style="color: #202020;">Employee</span>;
&nbsp;
<span style="color: #0000FF;">END</span></pre></td></tr></table></div>

<ul>
<li>Ordinarily this change would break an <a href="http://www.microsoft.com/sqlserver/2005/en/us/integration-services.aspx" target="_blank">SSIS</a> package. Execute the package again and check the output file.</li>
</ul>
<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/02/AdventureWorks_Output_File_2.png"><img style="display: inline; border-width: 0px;" title="AdventureWorks Output File HumanResources.Employee" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/02/AdventureWorks_Output_File_2_thumb.png" border="0" alt="AdventureWorks Output File 2 thumb SSIS: Make your output files dynamic" width="244" height="176" /></a></p>
<ul>
<li>Let&#8217;s try another version of <strong>usp_test</strong>.</li>
</ul>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p616code16'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p61616"><td class="code" id="p616code16"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">USE</span> <span style="color: #808080;">&#91;</span>AdventureWorks<span style="color: #808080;">&#93;</span>
GO
<span style="color: #0000FF;">SET</span> ANSI_<span style="color: #808080;">NULL</span>S <span style="color: #0000FF;">ON</span>
GO
<span style="color: #0000FF;">SET</span> QUOTED_IDENTIFIER <span style="color: #0000FF;">ON</span>
GO
<span style="color: #0000FF;">ALTER</span> <span style="color: #0000FF;">PROCEDURE</span> <span style="color: #808080;">&#91;</span>dbo<span style="color: #808080;">&#93;</span>.<span style="color: #808080;">&#91;</span>usp_test<span style="color: #808080;">&#93;</span> <span style="color: #0000FF;">AS</span>
<span style="color: #0000FF;">BEGIN</span>
&nbsp;
	<span style="color: #0000FF;">SELECT</span> <span style="color: #FF0000;">'I can change my output files simply by changing this stored procedure!'</span>, <span style="color: #FF00FF;">GETDATE</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#41;</span>;
&nbsp;
<span style="color: #0000FF;">END</span></pre></td></tr></table></div>

<ul>
<li>Run the package and check the new output file.</li>
</ul>
<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/02/AdventureWorks_Output_File_3.png"><img style="display: inline; border: 0px;" title="AdventureWorks Output File usp_test" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/02/AdventureWorks_Output_File_3_thumb.png" border="0" alt="AdventureWorks Output File 3 thumb SSIS: Make your output files dynamic" width="244" height="176" /></a></p>
<p>OK, so we now have an SSIS package, producing output files, that copes with complete modifications to stored procedures with no other changes needed. This isn&#8217;t yet production ready as there are a few issues I need to resolve.</p>
<ul>
<li>The Script Task seems to buckle for XML data types giving an unsupported conversion error. (Use the Person.Contact table in the AdventureWorks database to view this issue).</li>
<li>The package does not seem to cope with a large number of rows. Execution appears to freeze for datasets containing somewhat over 1,000 rows. (Use the Person.Address table in the AdventureWorks database to view this issue). I&#8217;m wondering if this is due to size limitation with the System.Object type.</li>
</ul>
<p>I&#8217;ll update this post one I get around to resolving these issues.</p>
<p><strong>UPDATE</strong> &#8211; See <a href="http://www.youdidwhatwithtsql.com/ssis-make-your-output-file-dynamic-part-2/664">Part 2</a> to see how I resolved these issues.</p>
<p><map name='google_ad_map_616_a45beff5d2e172f6'>
<area shape='rect' href='http://imageads.googleadservices.com/pagead/imgclick/616?pos=0' coords='1,2,367,28' />
<area shape='rect' href='http://services.google.com/feedback/abg' coords='384,10,453,23'/></map>
<img usemap='#google_ad_map_616_a45beff5d2e172f6' border='0' src='http://imageads.googleadservices.com/pagead/ads?format=468x30_aff_img&amp;client=&amp;channel=&amp;output=png&amp;cuid=616&amp;url=http%3A%2F%2Fwww.youdidwhatwithtsql.com%2Fssis-make-your-output-files-dynamic%2F616' title="SSIS: Make your output files dynamic" alt=" SSIS: Make your output files dynamic" /></p><p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/ssis-make-your-output-files-dynamic/616">SSIS: Make your output files dynamic</a></p>
<div class="none"><div class="g-plusone" data-href="http://www.youdidwhatwithtsql.com/ssis-make-your-output-files-dynamic/616" size="standard" count="true"></div></div>]]></content:encoded>
			<wfw:commentRss>http://www.youdidwhatwithtsql.com/ssis-make-your-output-files-dynamic/616/feed</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Execute SQ&#8230; Procedure Task</title>
		<link>http://www.youdidwhatwithtsql.com/execute-sq-procedure-task/559</link>
		<comments>http://www.youdidwhatwithtsql.com/execute-sq-procedure-task/559#comments</comments>
		<pubDate>Tue, 19 Jan 2010 22:04:10 +0000</pubDate>
		<dc:creator>Rhys</dc:creator>
				<category><![CDATA[SSIS]]></category>
		<category><![CDATA[Execute SQL Task]]></category>

		<guid isPermaLink="false">http://www.youdidwhatwithtsql.com/execute-sq-procedure-task/559</guid>
		<description><![CDATA[I&#8217;ve been having a bit of a debate with some colleagues today about the Execute SQL Procedure Task in SSIS. Is it ok to enter raw SQL queries into this task or should everything be enclosed within a Stored Procedure? My view&#8230; For me, any day of the week, it should always be a proc. [...]<p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/execute-sq-procedure-task/559">Execute SQ&#8230; Procedure Task</a></p>
]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been having a bit of a debate with some colleagues today about the <a href="http://technet.microsoft.com/en-us/library/ms141003.aspx" target="_blank">Execute <strike>SQL</strike> Procedure Task</a> in <a href="http://www.microsoft.com/sqlserver/2005/en/us/integration-services.aspx" target="_blank">SSIS</a>. Is it ok to enter raw SQL queries into this task or should everything be enclosed within a <a href="http://en.wikipedia.org/wiki/Stored_procedure" target="_blank">Stored Procedure</a>? My view&#8230;</p>
<table border="0" cellspacing="0" cellpadding="2" width="400">
<tbody>
<tr>
<td valign="top" width="200"><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/01/execute_sql_task_bad.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="execute sql task bad" border="0" alt="execute sql task bad thumb Execute SQ... Procedure Task" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/01/execute_sql_task_bad_thumb.png" width="244" height="209" /></a> </td>
<td valign="top" width="200"><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/01/red_x.gif"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="red x" border="0" alt="red x thumb Execute SQ... Procedure Task" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/01/red_x_thumb.gif" width="200" height="200" /></a> </td>
</tr>
</tbody>
</table>
<table border="0" cellspacing="0" cellpadding="2" width="400">
<tbody>
<tr>
<td valign="top" width="200"><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/01/execute_procedure_task_good.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="execute_procedure_task_good" border="0" alt="execute procedure task good thumb Execute SQ... Procedure Task" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/01/execute_procedure_task_good_thumb.png" width="244" height="208" /></a> </td>
<td valign="top" width="200"><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/01/green_tick.jpg"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="green tick" border="0" alt="green tick thumb Execute SQ... Procedure Task" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/01/green_tick_thumb.jpg" width="204" height="204" /></a> </td>
</tr>
</tbody>
</table>
</p>
<p>For me, any day of the week, it should always be a proc. Why on earth would you want to enter raw queries into this task? I would argue against this even for basic message logging. Enclosing your TSQL in stored procedures gives you much more flexibility. It&#8217;s so much easier to make minor modifications to a proc. Would you rather have to go through the rigmarole of opening and deploying SSIS packages. Sure, I know you&#8217;ll have to open the package if you introduce, or remove, new columns. But you&#8217;re pretty much free to modify your WHERE without issue. Lets not forget this also allows your system to be modified by those who don&#8217;t know SSIS. Perhaps you&#8217;ll be able to enjoy your holiday a little better next year.</p>
<p>I&#8217;m even tempted to open a <a href="http://connect.microsoft.com/" target="_blank">Microsoft Connect</a> item to fix this &#8216;bug&#8217;. Perhaps one day I will be lucky enough to see this task available in my toolbox.</p>
<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/01/execute_procedure_task.png"><img style="border-bottom: 0px; border-left: 0px; display: block; float: none; margin-left: auto; border-top: 0px; margin-right: auto; border-right: 0px" title="execute_procedure_task" border="0" alt="execute procedure task thumb Execute SQ... Procedure Task" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/01/execute_procedure_task_thumb.png" width="222" height="72" /></a></p>
<p><map name='google_ad_map_559_a45beff5d2e172f6'>
<area shape='rect' href='http://imageads.googleadservices.com/pagead/imgclick/559?pos=0' coords='1,2,367,28' />
<area shape='rect' href='http://services.google.com/feedback/abg' coords='384,10,453,23'/></map>
<img usemap='#google_ad_map_559_a45beff5d2e172f6' border='0' src='http://imageads.googleadservices.com/pagead/ads?format=468x30_aff_img&amp;client=&amp;channel=&amp;output=png&amp;cuid=559&amp;url=http%3A%2F%2Fwww.youdidwhatwithtsql.com%2Fexecute-sq-procedure-task%2F559' title="Execute SQ... Procedure Task" alt=" Execute SQ... Procedure Task" /></p><p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/execute-sq-procedure-task/559">Execute SQ&#8230; Procedure Task</a></p>
<div class="none"><div class="g-plusone" data-href="http://www.youdidwhatwithtsql.com/execute-sq-procedure-task/559" size="standard" count="true"></div></div>]]></content:encoded>
			<wfw:commentRss>http://www.youdidwhatwithtsql.com/execute-sq-procedure-task/559/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Powershell Script Task for SSIS</title>
		<link>http://www.youdidwhatwithtsql.com/powershell-script-task-for-ssis/488</link>
		<comments>http://www.youdidwhatwithtsql.com/powershell-script-task-for-ssis/488#comments</comments>
		<pubDate>Sat, 26 Dec 2009 18:59:44 +0000</pubDate>
		<dc:creator>Rhys</dc:creator>
				<category><![CDATA[Powershell]]></category>
		<category><![CDATA[SSIS]]></category>

		<guid isPermaLink="false">http://www.youdidwhatwithtsql.com/powershell-script-task-for-ssis/488</guid>
		<description><![CDATA[SSIS and Powershell are two of my current loves in technology, so of course, I was excited to see someone has made a Powershell Script Task. I&#8217;ve been meaning to try this out for months. Unfortunately it looks like the project isn&#8217;t currently active, but I thought I&#8217;d still give it a whirl, and post [...]<p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/powershell-script-task-for-ssis/488">Powershell Script Task for SSIS</a></p>
]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.microsoft.com/sqlserver/2005/en/us/integration-services.aspx" target="_blank">SSIS</a> and <a href="http://www.microsoft.com/windowsserver2003/technologies/management/powershell/default.mspx" target="_blank">Powershell</a> are two of my current loves in technology, so of course, I was excited to see someone has made a <a href="http://powershellscripttask.codeplex.com/" target="_blank">Powershell Script Task</a>. I&#8217;ve been meaning to try this out for months. Unfortunately it looks like the project isn&#8217;t currently active, but I thought I&#8217;d still give it a whirl, and post it here hoping to save others a little time on setup.</p>
<p>First you&#8217;ll need to download the <a href="http://powershellscripttask.codeplex.com/SourceControl/list/changesets" target="_blank">source code</a> as there&#8217;s no installation package. Then you&#8217;ll need to compile the project to produce the <a href="http://en.wikipedia.org/wiki/Dynamic-link_library" target="_blank">dll</a>. I used Visual Studio 2008 but I guess <a href="http://www.microsoft.com/express/download/" target="_blank">Visual C# Express Edition</a> will do it.</p>
<p>Double click the file called <strong>Defiant.SqlServer.PowerShellScriptTask.csproj</strong> from the source code you downloaded. This will open the project in your IDE. Build the project and a <strong>bin\debug</strong>&#160; directory containing a dll will be created in the project folder. </p>
<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/12/bin_debug_folder.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="bin debug folder" border="0" alt="bin debug folder thumb Powershell Script Task for SSIS" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/12/bin_debug_folder_thumb.png" width="644" height="116" /></a> </p>
<p>We are only interested in the file called <strong>Defiant.SqServer.PowerShellScriptTask.dll</strong>. We will need to register this file in the <a href="http://en.wikipedia.org/wiki/Global_Assembly_Cache" target="_blank">GAC</a> and copy it to a place where <a href="http://www.microsoft.com/sqlserver/2005/en/us/integration-services.aspx" target="_blank">SSIS</a> can find it. Save the commands below, with appropriate modifications, to a <a href="http://en.wikipedia.org/wiki/Batch_file" target="_blank">batch file</a>.</p>
<pre>cd\
c:
cd C:\Program Files\Microsoft SDKs\Windows\v6.0A\bin
gacutil /uf "Defiant.SqlServer.PowerShellScriptTask";
gacutil /if "C:\Users\Rhys\Desktop\Source\Defaint.SqlServer.PowerShellScriptTask\bin\Debug\Defiant.SqlServer.PowerShellScriptTask.dll";
copy "C:\Users\Rhys\Desktop\Source\Defaint.SqlServer.PowerShellScriptTask\bin\Debug\Defiant.SqlServer.PowerShellScriptTask.dll"; "C:\Program Files\Microsoft SQL Server\100\DTS\Tasks";</pre>
<p>You may need to modify; the path on line 3, this is the location of <a href="http://msdn.microsoft.com/en-us/library/ex0ss12c(VS.80).aspx" target="_blank">gacutil</a>. line5, this is the full path of <strong>Defiant.SqlServer.PowerShellScriptTask.dll</strong>. line 6 the full path of <strong>Defiant.SqlServer.PowerShellScriptTask.dll </strong>and the path to <strong>DTS\Tasks </strong>for the instance on <a href="http://www.microsoft.com/sqlserver/2008/en/us/default.aspx" target="_blank">SQL Server</a> you want to copy it to. For some reason I couldn&#8217;t get this to work on 2005 but it worked fine for 2008. Restart the SSIS service using services.msc then we are ready to fire up <a href="http://msdn.microsoft.com/en-us/library/ms173767.aspx" target="_blank">BIDS</a>. </p>
<p>When inside BIDS the Powershell Script Task will not appear in the toolbox automatically. Right click on the toolbox and select <strong>Choose Items</strong>. In the SSIS Control Flow Items tab scroll down to Powershell Script Task and check the box next to it.</p>
<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/12/choose_toolbox_items.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="choose toolbox items" border="0" alt="choose toolbox items thumb Powershell Script Task for SSIS" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/12/choose_toolbox_items_thumb.png" width="644" height="274" /></a> </p>
<p>Now the task will appear in the toolbox. Drop the task onto the designer to begin working with it.</p>
<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/12/Powershell_Script_Task.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="Powershell Script Task" border="0" alt="Powershell Script Task thumb Powershell Script Task for SSIS" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/12/Powershell_Script_Task_thumb.png" width="244" height="130" /></a> </p>
</p>
<p>Right click the task and choose edit. Anyone who has worked with the VB.Net \ C# Script Task should work this out straight away.</p>
<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/12/Powershell_Script_Task_Editor.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="Powershell Script Task Editor" border="0" alt="Powershell Script Task Editor thumb Powershell Script Task for SSIS" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/12/Powershell_Script_Task_Editor_thumb.png" width="644" height="423" /></a> </p>
<p>Let&#8217;s try something simple first. Enter the below line of Powershell in the Script window;</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p488code19'); return false;">View Code</a> POWERSHELL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p48819"><td class="code" id="p488code19"><pre class="powershell" style="font-family:monospace;"><span style="color: #008080; font-weight: bold;">Write-Host</span> <span style="color: #800000;">&quot;This is a test!&quot;</span>;</pre></td></tr></table></div>

<p>Click OK and then run the package. Below is the output I received.</p>
<pre>SSIS package &quot;Package.dtsx&quot; starting.
Information: 0x0 at PowerShell Script Task, PowerShellScriptTask: This is a test!
Information: 0x0 at PowerShell Script Task, PowerShellScriptTask:
SSIS package &quot;Package.dtsx&quot; finished: Success.</pre>
<p>Great, it works! Now lets try working with some variables. Add two variables like below (substituting your name for mine);</p>
<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/12/ssis_variable_powershell.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="ssis variable powershell" border="0" alt="ssis variable powershell thumb Powershell Script Task for SSIS" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/12/ssis_variable_powershell_thumb.png" width="644" height="169" /></a> </p>
<p>Then edit the Powershell Script Task and add these variables as below.</p>
<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/12/ssis_powershell_script_task_variables.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="ssis powershell script task variables" border="0" alt="ssis powershell script task variables thumb Powershell Script Task for SSIS" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/12/ssis_powershell_script_task_variables_thumb.png" width="644" height="423" /></a> </p>
<p>Add the below Powershell to the Script window;</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p488code20'); return false;">View Code</a> POWERSHELL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p48820"><td class="code" id="p488code20"><pre class="powershell" style="font-family:monospace;"><span style="color: #800080;">$ssis</span>.Variables<span style="color: #000000;">&#91;</span><span style="color: #800000;">&quot;User::write&quot;</span><span style="color: #000000;">&#93;</span> <span style="color: pink;">=</span> <span style="color: #800080;">$ssis</span>.Variables<span style="color: #000000;">&#91;</span><span style="color: #800000;">&quot;User::readOnly&quot;</span><span style="color: #000000;">&#93;</span>;</pre></td></tr></table></div>

<p>This code just assigns the value of the <strong>readOnly </strong>variable to <strong>write</strong>. Next drop a VB.net Script task onto the designer and connect the Powershell Script Task to it. We&#8217;re going to add code here to display a message box with the contents of the <strong>write </strong>variable. This will show that our Powershell script task has successfully read from, and written to, our SSIS variables.</p>
<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/12/script_task_edit.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="script task edit" border="0" alt="script task edit thumb Powershell Script Task for SSIS" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/12/script_task_edit_thumb.png" width="570" height="484" /></a> </p>
<p>Click edit script and and the below VB.Net code.</p>
<pre lang="VB.NET">Imports System
Imports System.Data
Imports System.Math
Imports Microsoft.SqlServer.Dts.Runtime

<system.addin.addin ("SCRIPTMAIN", ) Description:="" , Publisher:="" Version:="1.0"> _
<system.clscompliantattribute (FALSE)> _
Partial Public Class ScriptMain
	Inherits Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase

	Enum ScriptResults
		Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success
		Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure
	End Enum

	Public Sub Main()
        Dts.Variables("write").Value = Dts.Variables("readOnly").Value.ToString
        MsgBox("Hello " &#038; Dts.Variables("write").Value.ToString)
        Dts.TaskResult = ScriptResults.Success
    End Sub

End Class</pre>
<p>Your final package should look something like below.</p>
<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/12/powershell_final_ssis_package.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="powershell_final_ssis_package" border="0" alt="powershell final ssis package thumb Powershell Script Task for SSIS" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/12/powershell_final_ssis_package_thumb.png" width="184" height="198" /></a></p>
<p>Execute the package and, if all goes well, you should see the below message box.</p>
<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/12/MsgBox_Powershell_Script_Task.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="MsgBox_Powershell_Script_Task" border="0" alt="MsgBox Powershell Script Task thumb Powershell Script Task for SSIS" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/12/MsgBox_Powershell_Script_Task_thumb.png" width="244" height="121" /></a> </p>
</p>
<p>So that&#8217;s the basics of working with the Powershell Script Task. I&#8217;ve got over my deep hatred of VB.net, since working with it in SSIS so much, but Powershell is a welcome addition to my box of tricks! I&#8217;m looking forward to deploying this in future projects. It&#8217;s a shame the project seems inactive but this is really something I&#8217;d like to see <a href="http://www.microsoft.com/en/us/default.aspx" target="_blank">Microsoft</a> provide out-of-the-box in <a href="http://www.microsoft.com/sqlserver/2005/en/us/integration-services.aspx" target="_blank">SSIS</a>. </p>
<p><map name='google_ad_map_488_a45beff5d2e172f6'>
<area shape='rect' href='http://imageads.googleadservices.com/pagead/imgclick/488?pos=0' coords='1,2,367,28' />
<area shape='rect' href='http://services.google.com/feedback/abg' coords='384,10,453,23'/></map>
<img usemap='#google_ad_map_488_a45beff5d2e172f6' border='0' src='http://imageads.googleadservices.com/pagead/ads?format=468x30_aff_img&amp;client=&amp;channel=&amp;output=png&amp;cuid=488&amp;url=http%3A%2F%2Fwww.youdidwhatwithtsql.com%2Fpowershell-script-task-for-ssis%2F488' title="Powershell Script Task for SSIS" alt=" Powershell Script Task for SSIS" /></p><p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/powershell-script-task-for-ssis/488">Powershell Script Task for SSIS</a></p>
<div class="none"><div class="g-plusone" data-href="http://www.youdidwhatwithtsql.com/powershell-script-task-for-ssis/488" size="standard" count="true"></div></div>]]></content:encoded>
			<wfw:commentRss>http://www.youdidwhatwithtsql.com/powershell-script-task-for-ssis/488/feed</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>Automated Date Range Testing of SSIS Packages</title>
		<link>http://www.youdidwhatwithtsql.com/automated-date-range-testing-of-ssis-packages/466</link>
		<comments>http://www.youdidwhatwithtsql.com/automated-date-range-testing-of-ssis-packages/466#comments</comments>
		<pubDate>Sun, 06 Dec 2009 19:40:52 +0000</pubDate>
		<dc:creator>Rhys</dc:creator>
				<category><![CDATA[SSIS]]></category>
		<category><![CDATA[Powershell]]></category>
		<category><![CDATA[SQL Server]]></category>

		<guid isPermaLink="false">http://www.youdidwhatwithtsql.com/automated-date-range-testing-of-ssis-packages/466</guid>
		<description><![CDATA[I&#8217;m currently building a lot of SSIS packages that are primarily date driven. Many of these involve periods of 100 days, to several years, so I wanted to automate the testing of these packages. I&#8217;d previously automated the testing of stored procedures over date ranges but wanted a solution for testing the system as a [...]<p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/automated-date-range-testing-of-ssis-packages/466">Automated Date Range Testing of SSIS Packages</a></p>
]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m currently building a lot of <a href="http://www.microsoft.com/sqlserver/2005/en/us/integration-services.aspx" target="_blank">SSIS</a> packages that are primarily date driven. Many of these involve periods of 100 days, to several years, so I wanted to automate the testing of these packages. I&#8217;d previously automated the <a href="http://www.youdidwhatwithtsql.com/testing-datetime-dependent-stored-procedures/433" target="_blank">testing of stored procedures over date ranges</a> but wanted a solution for testing the system as a whole. The solution I came up with involves the use of <a href="http://www.microsoft.com/windowsserver2003/technologies/management/powershell/default.mspx" target="_blank">Powershell</a>. Essentially this script increments the date, by one day, before executing a package with <a href="http://msdn.microsoft.com/en-us/library/ms162810.aspx" target="_blank">dtexec</a>.</p>
<p>First lets create a simple <a href="http://www.microsoft.com/sqlserver/2005/en/us/integration-services.aspx" target="_blank">SSIS</a> package that the Powershell script will call. Open <a href="http://msdn.microsoft.com/en-us/library/ms173767.aspx" target="_blank">BIDS</a> and create a new project. Add a string variable to the project called <strong>sql_date </strong>like below.</p>
<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/12/sql_datevariable.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="sql_date variable" border="0" alt="sql datevariable thumb Automated Date Range Testing of SSIS Packages" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/12/sql_datevariable_thumb.png" width="644" height="183" /></a> </p>
<p>We&#8217;re going to use this variable to get the date as <a href="http://www.microsoft.com/sqlserver/2008/en/us/default.aspx" target="_blank">SQL Server</a> recognises it so we know it&#8217;s the same as the system date. During my testing I discovered that SQL Server would take a few seconds pickup a system date change. Obviously this could distort your results so we need to ensure these are the same.</p>
<p>Next add an <a href="http://technet.microsoft.com/en-us/library/ms141003.aspx" target="_blank">Execute SQL Task</a>, from the toolbox, onto the designer. Call it &quot;Get SQL Server Date&quot;. Add a connection to the SQL Server your package will be executing against and change the <strong>ResultSet </strong>property to &quot;Single Row&quot;. Add the following TSQL to <strong>SQLStatement</strong>.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p466code23'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p46623"><td class="code" id="p466code23"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">SELECT</span> <span style="color: #0000FF;">CONVERT</span><span style="color: #808080;">&#40;</span><span style="color: #0000FF;">VARCHAR</span>, <span style="color: #FF00FF;">GETDATE</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#41;</span>, <span style="color: #000;">103</span><span style="color: #808080;">&#41;</span>;</pre></td></tr></table></div>

<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/12/execute_sql_task_date.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="execute_sql_task_date" border="0" alt="execute sql task date thumb Automated Date Range Testing of SSIS Packages" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/12/execute_sql_task_date_thumb.png" width="244" height="208" /></a> </p>
<p>Next add a <a href="http://msdn.microsoft.com/en-us/library/ms141752.aspx" target="_blank">Script Task</a> onto the designer and call it <strong>MsgBoxDate</strong>. Right click the task, click edit, add <strong>sql_date</strong> to <strong>ReadOnlyVariables</strong> on the script tab. </p>
<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/12/script_task.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="script_task" border="0" alt="script task thumb Automated Date Range Testing of SSIS Packages" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/12/script_task_thumb.png" width="244" height="208" /></a> </p>
<p>Click &quot;Design Script&quot; and add the below code into the ide.</p>
<pre lang="VB.NET">Imports System
Imports System.Data
Imports System.Math
Imports Microsoft.SqlServer.Dts.Runtime

Public Class ScriptMain

    Public Sub Main()
        Dim sql_date As String = Dts.Variables("sql_date").Value.ToString
        Dim msg As String = "SQL Server Date = " &#038; sql_date &#038; Environment.NewLine &#038; "System Date = " &#038; Format(Date.Now, "dd/MM/yyyy")
        MsgBox(msg)
        Dts.TaskResult = Dts.Results.Success
    End Sub

End Class</pre>
<p>Connect the tasks together with a precedence constraint. The package should look something like below.</p>
<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/12/date_package.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="date_package" border="0" alt="date package thumb Automated Date Range Testing of SSIS Packages" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/12/date_package_thumb.png" width="244" height="197" /></a> </p>
<p>Return to the SSIS Designer, click File &gt; Save Copy of Package.dtsx As. Save this package in the file system and remember the path as you&#8217;ll need it later. Below is the <a href="http://www.microsoft.com/windowsserver2003/technologies/management/powershell/default.mspx" target="_blank">Powershell</a> script that will change the current day, by one day, and then execute the package we just made. The while loop will verify that SQL Server has picked up the system date change before executing the package. Change the settings for <strong>$sqlserver </strong>and the package path for <a href="http://msdn.microsoft.com/en-us/library/ms162810.aspx" target="_blank">dtexec</a> as appropriate. The <strong>$days </strong>variable should be changed to the number of days you want to run the package for. In this example it&#8217;s just ten. The script will set your systems date back once it has successfully completed.. </p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p466code24'); return false;">View Code</a> POWERSHELL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p46624"><td class="code" id="p466code24"><pre class="powershell" style="font-family:monospace;"><span style="color: #008000;"># SQL server and database settings</span>
<span style="color: #800080;">$sqlserver</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;localhost\sql2005&quot;</span>;
<span style="color: #800080;">$database</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;master&quot;</span>;
<span style="color: #008000;"># Record the current date so we can set it back at the end of the script</span>
<span style="color: #008000;"># You may need to change this depending on your regional settings</span>
<span style="color: #800080;">$date</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">Get-Date</span> <span style="color: #008080; font-style: italic;">-Format</span> <span style="color: #800000;">&quot;dd/MM/yyyy&quot;</span>;
&nbsp;
<span style="color: #008000;"># Connection string used for verifying sql server has registered the date change</span>
<span style="color: #800080;">$connection_string</span> <span style="color: pink;">=</span> <span style="color: #000000;">&#40;</span><span style="color: #800000;">&quot;Data Source=$sqlserver;Initial Catalog=$database;Integrated Security=SSPI&quot;</span><span style="color: #000000;">&#41;</span>
<span style="color: #800080;">$days</span> <span style="color: pink;">=</span> <span style="color: #804000;">10</span>;
&nbsp;
<span style="color: #008000;"># Setup the database connection</span>
<span style="color: #800080;">$conn</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">New-Object</span> System.Data.SqlClient.SqlConnection<span style="color: #000000;">&#40;</span><span style="color: #800080;">$connection_string</span><span style="color: #000000;">&#41;</span>;
<span style="color: #800080;">$conn</span>.Open<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
<span style="color: #800080;">$cmd</span> <span style="color: pink;">=</span> <span style="color: #800080;">$conn</span>.CreateCommand<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
<span style="color: #800080;">$cmd</span>.CommandText <span style="color: pink;">=</span> <span style="color: #800000;">&quot;SELECT DATEPART(dd, GETDATE())&quot;</span>;
&nbsp;
&nbsp;
<span style="color: #0000FF;">for</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$i</span> <span style="color: pink;">=</span> <span style="color: #804000;">0</span>; <span style="color: #800080;">$i</span> <span style="color: #FF0000;">-lt</span> <span style="color: #800080;">$days</span>; <span style="color: #800080;">$i</span><span style="color: pink;">++</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
	<span style="color: #008000;"># Change the date by one day</span>
	<span style="color: #008080; font-weight: bold;">Set-Date</span> <span style="color: #000000;">&#40;</span><span style="color: #008080; font-weight: bold;">Get-Date</span><span style="color: #000000;">&#41;</span>.AddDays<span style="color: #000000;">&#40;</span><span style="color: #804000;">1</span><span style="color: #000000;">&#41;</span>;
	<span style="color: #008000;"># SQL Server can take a few seconds to register the change</span>
	<span style="color: #008000;"># so we need to loop until the day matches before </span>
	<span style="color: #008000;"># executing our ssis package</span>
	<span style="color: #800080;">$reader</span> <span style="color: pink;">=</span> <span style="color: #800080;">$cmd</span>.ExecuteReader<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
	<span style="color: #800080;">$reader</span>.Read<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
	<span style="color: #800080;">$sql_day</span> <span style="color: pink;">=</span> <span style="color: #800080;">$reader</span>.GetValue<span style="color: #000000;">&#40;</span><span style="color: #804000;">0</span><span style="color: #000000;">&#41;</span>;
	<span style="color: #800080;">$reader</span>.Close<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
	<span style="color: #0000FF;">while</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$sql_day</span><span style="color: #000000;">&#41;</span> <span style="color: #FF0000;">-ne</span> <span style="color: #000000;">&#91;</span>DateTime<span style="color: #000000;">&#93;</span>::Now.Day<span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		<span style="color: #008080; font-weight: bold;">Start-Sleep</span> <span style="color: #008080; font-style: italic;">-Seconds</span> <span style="color: #804000;">2</span>;
		<span style="color: #008000;"># Check the sql day again</span>
		<span style="color: #800080;">$reader</span> <span style="color: pink;">=</span> <span style="color: #800080;">$cmd</span>.ExecuteReader<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
		<span style="color: #800080;">$reader</span>.Read<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
		<span style="color: #800080;">$sql_day</span> <span style="color: pink;">=</span> <span style="color: #800080;">$reader</span>.GetValue<span style="color: #000000;">&#40;</span><span style="color: #804000;">0</span><span style="color: #000000;">&#41;</span>;
		<span style="color: #800080;">$reader</span>.Close<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
	<span style="color: #000000;">&#125;</span>
	<span style="color: #008000;"># Execute the package for this date</span>
	dtexec <span style="color: pink;">/</span>f <span style="color: #800000;">&quot;C:\Users\Rhys\Desktop\Package.dtsx&quot;</span>
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #008000;"># Clean up</span>
<span style="color: #800080;">$reader</span>.Close<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
<span style="color: #800080;">$conn</span>.Close<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
&nbsp;
<span style="color: #008000;"># Put the date back with the current time</span>
<span style="color: #800080;">$time</span> <span style="color: pink;">=</span> <span style="color: #000000;">&#91;</span>DateTime<span style="color: #000000;">&#93;</span>::Now.TimeOfDay;
<span style="color: #008080; font-weight: bold;">Set-Date</span> <span style="color: #800000;">&quot;$date $time&quot;</span>;</pre></td></tr></table></div>

<p>The script will work through each day displaying a message box for each iteration.</p>
<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/12/start_date.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="start_date" border="0" alt="start date thumb Automated Date Range Testing of SSIS Packages" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/12/start_date_thumb.png" width="215" height="163" /></a> </p>
<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/12/end_date.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="end_date" border="0" alt="end date thumb Automated Date Range Testing of SSIS Packages" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/12/end_date_thumb.png" width="215" height="163" /></a> </p>
</p>
<p>Obviously this particular package needs human intervention to get it to complete. Your real-life packages wouldn&#8217;t have this limitation so this script could be used to test over a large date range. Hopefully this script will allow me to get out of the office a bit earlier when I need to test my packages!</p>
<p><map name='google_ad_map_466_a45beff5d2e172f6'>
<area shape='rect' href='http://imageads.googleadservices.com/pagead/imgclick/466?pos=0' coords='1,2,367,28' />
<area shape='rect' href='http://services.google.com/feedback/abg' coords='384,10,453,23'/></map>
<img usemap='#google_ad_map_466_a45beff5d2e172f6' border='0' src='http://imageads.googleadservices.com/pagead/ads?format=468x30_aff_img&amp;client=&amp;channel=&amp;output=png&amp;cuid=466&amp;url=http%3A%2F%2Fwww.youdidwhatwithtsql.com%2Fautomated-date-range-testing-of-ssis-packages%2F466' title="Automated Date Range Testing of SSIS Packages" alt=" Automated Date Range Testing of SSIS Packages" /></p><p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/automated-date-range-testing-of-ssis-packages/466">Automated Date Range Testing of SSIS Packages</a></p>
<div class="none"><div class="g-plusone" data-href="http://www.youdidwhatwithtsql.com/automated-date-range-testing-of-ssis-packages/466" size="standard" count="true"></div></div>]]></content:encoded>
			<wfw:commentRss>http://www.youdidwhatwithtsql.com/automated-date-range-testing-of-ssis-packages/466/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>SSIS: Don&#8217;t run the process on a bank Holiday</title>
		<link>http://www.youdidwhatwithtsql.com/ssis-dont-run-the-process-on-a-bank-holiday/452</link>
		<comments>http://www.youdidwhatwithtsql.com/ssis-dont-run-the-process-on-a-bank-holiday/452#comments</comments>
		<pubDate>Sat, 21 Nov 2009 20:45:13 +0000</pubDate>
		<dc:creator>Rhys</dc:creator>
				<category><![CDATA[SSIS]]></category>
		<category><![CDATA[Bank Holidays]]></category>
		<category><![CDATA[SQL Server 2008]]></category>

		<guid isPermaLink="false">http://www.youdidwhatwithtsql.com/ssis-dont-run-the-process-on-a-bank-holiday/452</guid>
		<description><![CDATA[Sadly, as people don&#8217;t make sense, we have to make compromises in our systems and processes. I recently had a requirement, in an SSIS package, to be able to identify which days were Bank Holidays and take a different course of action, e.g. not run the main process. Here&#8217;s an illustration of the approach I [...]<p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/ssis-dont-run-the-process-on-a-bank-holiday/452">SSIS: Don&#8217;t run the process on a bank Holiday</a></p>
]]></description>
			<content:encoded><![CDATA[<p>Sadly, as people don&#8217;t make sense, we have to make compromises in our systems and processes. I recently had a requirement, in an <a href="http://msdn.microsoft.com/en-us/library/ms141026.aspx" target="_blank">SSIS</a> package, to be able to identify which days were Bank Holidays and take a different course of action, e.g. not run the main process. Here&#8217;s an illustration of the approach I took using a simple lookup table, containing holiday dates, and SSIS <a href="http://technet.microsoft.com/en-us/library/ms141261.aspx" target="_blank">precedence constraints</a>.</p>
<p>The TSQL (<a href="http://www.microsoft.com/sqlserver/2008/en/us/default.aspx" target="_blank">SQL Server</a> 2008) below will create a table called <strong>BankHolidays</strong> and populate the table with public holidays up to the end of 2010. The Bank Holiday dates have been taken from the <a href="http://www.direct.gov.uk/en/Governmentcitizensandrights/LivingintheUK/DG_073741" target="_blank">DirectGov</a> site and are for England &amp; Wales only. Depending on your requirements you may want to consider dates, like Christmas, that fall on a weekend. These technically aren&#8217;t Bank Holidays so haven&#8217;t been included here, but you may wish to add them if you don&#8217;t want to run on these days.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p452code28'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p45228"><td class="code" id="p452code28"><pre class="tsql" style="font-family:monospace;"><span style="color: #008080;">-- Create a table to contain Bank Holidays</span>
<span style="color: #0000FF;">CREATE</span> <span style="color: #0000FF;">TABLE</span> dbo.<span style="color: #202020;">BankHolidays</span>
<span style="color: #808080;">&#40;</span>
	BankHoliday <span style="color: #0000FF;">DATE</span> <span style="color: #808080;">NOT</span> <span style="color: #808080;">NULL</span> <span style="color: #0000FF;">PRIMARY</span> <span style="color: #0000FF;">KEY</span> <span style="color: #0000FF;">CLUSTERED</span>,
	ActiVe <span style="color: #0000FF;">BIT</span> <span style="color: #808080;">NOT</span> <span style="color: #808080;">NULL</span> <span style="color: #0000FF;">DEFAULT</span> <span style="color: #000;">1</span>
<span style="color: #808080;">&#41;</span>;
GO
&nbsp;
<span style="color: #0000FF;">INSERT</span> <span style="color: #0000FF;">INTO</span> dbo.<span style="color: #202020;">BankHolidays</span>
<span style="color: #808080;">&#40;</span>
	BankHoliday
<span style="color: #808080;">&#41;</span>
<span style="color: #0000FF;">VALUES</span>
<span style="color: #808080;">&#40;</span>
	<span style="color: #FF0000;">'2009-12-25'</span>
<span style="color: #808080;">&#41;</span>,
<span style="color: #808080;">&#40;</span>
	<span style="color: #FF0000;">'2009-12-28'</span>
<span style="color: #808080;">&#41;</span>,
<span style="color: #808080;">&#40;</span>
	<span style="color: #FF0000;">'2010-01-01'</span>
<span style="color: #808080;">&#41;</span>,
<span style="color: #808080;">&#40;</span>
	<span style="color: #FF0000;">'2010-04-02'</span>
<span style="color: #808080;">&#41;</span>,
<span style="color: #808080;">&#40;</span>
	<span style="color: #FF0000;">'2010-04-05'</span>
<span style="color: #808080;">&#41;</span>,
<span style="color: #808080;">&#40;</span>
	<span style="color: #FF0000;">'2010-05-03'</span>
<span style="color: #808080;">&#41;</span>,
<span style="color: #808080;">&#40;</span>
	<span style="color: #FF0000;">'2010-05-31'</span>
<span style="color: #808080;">&#41;</span>,
<span style="color: #808080;">&#40;</span>
	<span style="color: #FF0000;">'2010-08-30'</span>
<span style="color: #808080;">&#41;</span>,
<span style="color: #808080;">&#40;</span>
	<span style="color: #FF0000;">'2010-12-27'</span>
<span style="color: #808080;">&#41;</span>,
<span style="color: #808080;">&#40;</span>
	<span style="color: #FF0000;">'2010-12-28'</span>
<span style="color: #808080;">&#41;</span>;
GO</pre></td></tr></table></div>

<p>Note the <strong>Active</strong> flag in the table. This is just in case we need to deactivate a Bank Holiday for some some reason. Added flexibility is always useful.</p>
<p>Launch <a href="http://msdn.microsoft.com/en-us/library/ms173767.aspx" target="_blank">BIDS</a> and create a new <a href="http://www.microsoft.com/sqlserver/2005/en/us/integration-services.aspx" target="_blank">SSIS</a> project. The first thing we need to do is add a variable called <strong>BankHoliday</strong>. We will use this in determining whether the day is a Bank Holiday or not. Add an Int32 variable in the <a href="http://www.microsoft.com/sqlserver/2005/en/us/integration-services.aspx" target="_blank">SSIS</a> variables window, as illustrated below.</p>
<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/11/ssis_bank_holiday_variable.png"><img style="display: inline; border-width: 0px;" title="ssis bank holiday variable" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/11/ssis_bank_holiday_variable_thumb.png" border="0" alt="ssis bank holiday variable thumb SSIS: Dont run the process on a bank Holiday" width="644" height="167" /></a></p>
<p>Add an <a href="http://msdn.microsoft.com/en-us/library/ms141013.aspx" target="_blank">OLE DB connection</a> that points at the database containing the <strong>BankHolidays</strong> table. Drop an <a href="http://technet.microsoft.com/en-us/library/ms141003.aspx" target="_blank">Execute SQL task</a> from the toolbox onto the design canvas. Right click the task and click edit to configure the task properties. Add the OLE DB connection manager to <strong>Connection </strong>and the below TSQL to <strong>SQLStatement</strong>.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p452code29'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p45229"><td class="code" id="p452code29"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">SELECT</span> <span style="color: #FF00FF;">COUNT</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">*</span><span style="color: #808080;">&#41;</span>
<span style="color: #0000FF;">FROM</span> dbo.<span style="color: #202020;">BankHolidays</span>
<span style="color: #0000FF;">WHERE</span> BankHoliday <span style="color: #808080;">=</span> <span style="color: #0000FF;">CONVERT</span><span style="color: #808080;">&#40;</span><span style="color: #0000FF;">DATE</span>, <span style="color: #FF00FF;">GETDATE</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span>
<span style="color: #808080;">AND</span> Active <span style="color: #808080;">=</span> <span style="color: #000;">1</span>;</pre></td></tr></table></div>

<p><strong>ResultSet</strong> should be changed to &#8220;Single row&#8221;.</p>
<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/11/Execute_SQL_Task_Bank_Holidays.png"><img style="display: inline; border-width: 0px;" title="Execute SQL Task Bank Holidays" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/11/Execute_SQL_Task_Bank_Holidays_thumb.png" border="0" alt="Execute SQL Task Bank Holidays thumb SSIS: Dont run the process on a bank Holiday" width="244" height="208" /></a></p>
<p><strong>UPDATE:</strong> (Thanks to Dr Drew in the comments for pointing this omission out.)</p>
<p>Click on the <strong>Result Set </strong>tab and map the variable <strong>BankHoliday</strong> as shown below. This variable mapping will be used to determine if the execution date is a bank holiday.</p>
<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/11/resultset_mapping_bank_holiday.png"><img class="alignnone size-medium wp-image-859" title="resultset mapping bankholiday variable" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/11/resultset_mapping_bank_holiday-300x254.png" alt="resultset mapping bank holiday 300x254 SSIS: Dont run the process on a bank Holiday" width="300" height="254" /></a></p>
<p>Now drop two <a href="http://msdn.microsoft.com/en-us/library/ms141752.aspx" target="_blank">Script Task</a> components onto the design canvas and connect them from the Execute SQL Task. Call one &#8220;Display &#8220;Not Bank Holiday&#8221; message&#8221; and the other &#8220;Display &#8220;It&#8217;s a Bank Holiday&#8221; message&#8221;. This should look something like this.</p>
<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/11/ssis_design_canvas.png"><img style="display: inline; border-width: 0px;" title="ssis design canvas" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/11/ssis_design_canvas_thumb.png" border="0" alt="ssis design canvas thumb SSIS: Dont run the process on a bank Holiday" width="244" height="91" /></a></p>
<p>Next we need to edit the constraints connecting our Script tasks to implement the logic to determine if it is a Bank Holiday or not. Right click the constraint connecting the script task called &#8220;Display &#8220;Not Bank Holiday&#8221; message&#8221; and choose Edit. Change &#8220;Evaluation Operation&#8221; to &#8216;Expression and Constraint&#8217; and enter the following expression into the appropriate text box; @BankHoliday == 0. Finally click the radio button labelled &#8220;Logical OR&#8221;. This should look like this&#8230;</p>
<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/11/Bank_Holiday_Constraint_1.png"><img style="display: inline; border: 0px;" title="Bank_Holiday_Constraint_1" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/11/Bank_Holiday_Constraint_1_thumb.png" border="0" alt="Bank Holiday Constraint 1 thumb SSIS: Dont run the process on a bank Holiday" width="644" height="481" /></a></p>
<p>Click OK to save your changes. Now we need to edit the constraint connected to the Script Task called &#8220;Display &#8220;It&#8217;s a Bank Holiday&#8221; message&#8221;. The setup here is exactly the same except for the Expression which should be entered as @BankHoliday == 1.</p>
<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/11/Bank_Holiday_Constraint_2.png"><img style="display: inline; border: 0px;" title="Bank_Holiday_Constraint_2" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/11/Bank_Holiday_Constraint_2_thumb.png" border="0" alt="Bank Holiday Constraint 2 thumb SSIS: Dont run the process on a bank Holiday" width="644" height="481" /></a></p>
<p>By now your design canvas should look something like below.</p>
<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/11/ssis_design_canvas_2.png"><img style="display: inline; border: 0px;" title="ssis_design_canvas_2" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/11/ssis_design_canvas_2_thumb.png" border="0" alt="ssis design canvas 2 thumb SSIS: Dont run the process on a bank Holiday" width="244" height="90" /></a></p>
<p>Finally we&#8217;re just going to add a little code to each script task to display a message box. In the script task called &#8220;Display &#8220;Not Bank Holiday&#8221; message&#8221; click edit, go to the script tab, and click the &#8220;Design Script button&#8221;. Add the below code.</p>
<pre lang="VB.Net">Imports System
Imports System.Data
Imports System.Math
Imports Microsoft.SqlServer.Dts.Runtime

Public Class ScriptMain

    Public Sub Main()
        MsgBox("Hi, today is not a Bank Holiday, get back to work!")
        Dts.TaskResult = Dts.Results.Success
    End Sub

End Class</pre>
<p>In the script task called &#8220;Display &#8220;It&#8217;s a Bank Holiday&#8221; message&#8221; add the below code.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p452code30'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p45230"><td class="code" id="p452code30"><pre class="tsql" style="font-family:monospace;">Imports System
Imports System.<span style="color: #0000FF;">Data</span>
Imports System.<span style="color: #202020;">Math</span>
Imports Microsoft.<span style="color: #202020;">SqlServer</span>.<span style="color: #202020;">Dts</span>.<span style="color: #202020;">Runtime</span>
&nbsp;
<span style="color: #0000FF;">Public</span> <span style="color: #0000FF;">Class</span> ScriptMain
&nbsp;
	<span style="color: #0000FF;">Public</span> Sub Main<span style="color: #808080;">&#40;</span><span style="color: #808080;">&#41;</span>
        MsgBox<span style="color: #808080;">&#40;</span><span style="color: #FF0000;">&quot;Hi, today is a Bank Holiday, put your feet up!&quot;</span><span style="color: #808080;">&#41;</span>
		Dts.<span style="color: #202020;">TaskResult</span> <span style="color: #808080;">=</span> Dts.<span style="color: #202020;">Results</span>.<span style="color: #202020;">Success</span>
	<span style="color: #0000FF;">End</span> Sub
&nbsp;
<span style="color: #0000FF;">End</span> <span style="color: #0000FF;">Class</span></pre></td></tr></table></div>

<p>Now we should be ready to execute the package. Click run and you should see the following message (assuming it&#8217;s not a bank holiday!).</p>
<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/11/not_a_bank_holiday.png"><img style="display: inline; border: 0px;" title="not_a_bank_holiday" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/11/not_a_bank_holiday_thumb.png" border="0" alt="not a bank holiday thumb SSIS: Dont run the process on a bank Holiday" width="244" height="113" /></a></p>
<p>Now, either wait until a bank holiday occurs, or change the date on your computer to one contained in the <strong>BankHolidays</strong> table. I changed mine to 2009-12-28 and here&#8217;s what I saw when I executed the package.</p>
<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/11/bank_holiday.png"><img style="display: inline; border: 0px;" title="bank_holiday" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/11/bank_holiday_thumb.png" border="0" alt="bank holiday thumb SSIS: Dont run the process on a bank Holiday" width="244" height="123" /></a></p>
<p><map name='google_ad_map_452_a45beff5d2e172f6'>
<area shape='rect' href='http://imageads.googleadservices.com/pagead/imgclick/452?pos=0' coords='1,2,367,28' />
<area shape='rect' href='http://services.google.com/feedback/abg' coords='384,10,453,23'/></map>
<img usemap='#google_ad_map_452_a45beff5d2e172f6' border='0' src='http://imageads.googleadservices.com/pagead/ads?format=468x30_aff_img&amp;client=&amp;channel=&amp;output=png&amp;cuid=452&amp;url=http%3A%2F%2Fwww.youdidwhatwithtsql.com%2Fssis-dont-run-the-process-on-a-bank-holiday%2F452' title="SSIS: Dont run the process on a bank Holiday" alt=" SSIS: Dont run the process on a bank Holiday" /></p><p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/ssis-dont-run-the-process-on-a-bank-holiday/452">SSIS: Don&#8217;t run the process on a bank Holiday</a></p>
<div class="none"><div class="g-plusone" data-href="http://www.youdidwhatwithtsql.com/ssis-dont-run-the-process-on-a-bank-holiday/452" size="standard" count="true"></div></div>]]></content:encoded>
			<wfw:commentRss>http://www.youdidwhatwithtsql.com/ssis-dont-run-the-process-on-a-bank-holiday/452/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>SSIS: Writing to the Windows Event Log</title>
		<link>http://www.youdidwhatwithtsql.com/ssis-writing-to-the-windows-event-log/407</link>
		<comments>http://www.youdidwhatwithtsql.com/ssis-writing-to-the-windows-event-log/407#comments</comments>
		<pubDate>Thu, 22 Oct 2009 12:34:50 +0000</pubDate>
		<dc:creator>Rhys</dc:creator>
				<category><![CDATA[SSIS]]></category>
		<category><![CDATA[Event Log]]></category>
		<category><![CDATA[VB.Net]]></category>

		<guid isPermaLink="false">http://www.youdidwhatwithtsql.com/ssis-writing-to-the-windows-event-log/407</guid>
		<description><![CDATA[Here’s a quick VB.Net snippet that allows you to write messages to the Windows Event Log from your packages. Nothing to configure here, just change the log message to something appropriate. Obviously, in order to work, the package would have to be run under an account that has permission to write to the event log. [...]<p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/ssis-writing-to-the-windows-event-log/407">SSIS: Writing to the Windows Event Log</a></p>
]]></description>
			<content:encoded><![CDATA[<p>Here’s a quick <a href="http://en.wikipedia.org/wiki/Visual_Basic_.NET" target="_blank">VB.Net</a> snippet that allows you to write messages to the <a href="http://en.wikipedia.org/wiki/Event_Viewer#Windows_Event_Log" target="_blank">Windows Event Log</a> from your packages. Nothing to configure here, just change the log message to something appropriate. Obviously, in order to work, the package would have to be run under an account that has permission to write to the event log. Copy the below code into a <a href="http://msdn.microsoft.com/en-us/library/ms141752.aspx" target="_blank">Script Task</a>, change the message, and you’re ready to go.</p>
<pre lang="VB.NET">
' Microsoft SQL Server Integration Services Script Task
' Write scripts using Microsoft Visual Basic
' The ScriptMain class is the entry point of the Script Task.

Imports System
Imports System.Data
Imports System.Math
Imports Microsoft.SqlServer.Dts.Runtime
Imports System.Diagnostics.EventLog
Imports System.Diagnostics.EventLogEntryType

Public Class ScriptMain

	' The execution engine calls this method when the task executes.
	' To access the object model, use the Dts object. Connections, variables, events,
	' and logging features are available as static members of the Dts class.
	' Before returning from this method, set the value of Dts.TaskResult to indicate success or failure.
	'
	' To open Code and Text Editor Help, press F1.
	' To open Object Browser, press Ctrl+Alt+J.

    Public Sub Main()

        Dim eventLog As New Diagnostics.EventLog

        ' If the SSIS event log doesn't exist on the target machine then create it
        If Not eventLog.SourceExists("SSIS") Then
            eventLog.CreateEventSource("SSIS", "Application")
        End If

        ' Create an instance of the EventLog class
        eventLog = New Diagnostics.EventLog()
        ' Specify the source as SSIS
        eventLog.Source = "SSIS"
        ' Add an event log message
        eventLog.WriteEntry("This is a message from a Script Task!", Diagnostics.EventLogEntryType.Information)

        Dts.TaskResult = Dts.Results.Success
    End Sub

End Class
</pre>
<p>Execute the task and you should find the following message in the event viewer.</p>
<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/10/SSIS_Message_Event_Log.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="SSIS Message in the Windows Event Log" border="0" alt="SSIS Message Event Log thumb SSIS: Writing to the Windows Event Log" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/10/SSIS_Message_Event_Log_thumb.png" width="244" height="150" /></a></p>
<p><map name='google_ad_map_407_a45beff5d2e172f6'>
<area shape='rect' href='http://imageads.googleadservices.com/pagead/imgclick/407?pos=0' coords='1,2,367,28' />
<area shape='rect' href='http://services.google.com/feedback/abg' coords='384,10,453,23'/></map>
<img usemap='#google_ad_map_407_a45beff5d2e172f6' border='0' src='http://imageads.googleadservices.com/pagead/ads?format=468x30_aff_img&amp;client=&amp;channel=&amp;output=png&amp;cuid=407&amp;url=http%3A%2F%2Fwww.youdidwhatwithtsql.com%2Fssis-writing-to-the-windows-event-log%2F407' title="SSIS: Writing to the Windows Event Log" alt=" SSIS: Writing to the Windows Event Log" /></p><p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/ssis-writing-to-the-windows-event-log/407">SSIS: Writing to the Windows Event Log</a></p>
<div class="none"><div class="g-plusone" data-href="http://www.youdidwhatwithtsql.com/ssis-writing-to-the-windows-event-log/407" size="standard" count="true"></div></div>]]></content:encoded>
			<wfw:commentRss>http://www.youdidwhatwithtsql.com/ssis-writing-to-the-windows-event-log/407/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>SSIS: Archiving files with VB.Net</title>
		<link>http://www.youdidwhatwithtsql.com/ssis-archiving-files-with-vb-net/402</link>
		<comments>http://www.youdidwhatwithtsql.com/ssis-archiving-files-with-vb-net/402#comments</comments>
		<pubDate>Thu, 15 Oct 2009 21:06:37 +0000</pubDate>
		<dc:creator>Rhys</dc:creator>
				<category><![CDATA[SSIS]]></category>
		<category><![CDATA[archiving files]]></category>
		<category><![CDATA[VB.Net]]></category>

		<guid isPermaLink="false">http://www.youdidwhatwithtsql.com/ssis-archiving-files-with-vb-net/402</guid>
		<description><![CDATA[When creating SSIS packages it’s a common requirement to be able to archive the processed files. Here’s VB.Net code snippet, with a quick walkthrough, that does exactly that. The script uses two variables, inFiles and archiveFiles. The variable inFiles contains the folder we want to move files from, and archiveFiles is the destination. Configure your [...]<p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/ssis-archiving-files-with-vb-net/402">SSIS: Archiving files with VB.Net</a></p>
]]></description>
			<content:encoded><![CDATA[<p>When creating <a href="http://www.microsoft.com/sqlserver/2005/en/us/integration-services.aspx" target="_blank">SSIS</a> packages it’s a common requirement to be able to archive the processed files. Here’s <a href="http://en.wikipedia.org/wiki/Visual_Basic_.NET" target="_blank">VB.Net</a> code snippet, with a quick walkthrough, that does exactly that. </p>
<p>The script uses two variables, <strong>inFiles </strong>and <strong>archiveFiles</strong>. The variable <strong>inFiles</strong> contains the folder we want to move files from, and <strong>archiveFiles</strong> is the destination.</p>
<p>Configure your variables in <a href="http://msdn.microsoft.com/en-us/library/ms173767.aspx" target="_blank">BIDS</a> like below.</p>
<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/10/ssis_variables.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="ssis variables for archiving files" border="0" alt="ssis variables thumb SSIS: Archiving files with VB.Net" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/10/ssis_variables_thumb.png" width="644" height="133" /></a> </p>
<p>Add a <a href="http://msdn.microsoft.com/en-us/library/ms141752.aspx" target="_blank">script task</a> to the designer.</p>
<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/10/archive_files_script_task.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="Script task for archiving files in BIDS" border="0" alt="archive files script task thumb SSIS: Archiving files with VB.Net" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/10/archive_files_script_task_thumb.png" width="244" height="124" /></a> </p>
<p>Right click the script task, choose ‘Edit’ then ‘Script’. Add the <strong>inFiles </strong>and <strong>archiveFiles</strong> variables to the<strong> ReadOnlyVariables </strong>textbox.</p>
<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/10/script_task_config.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="Script Task configuration for archiving files" border="0" alt="script task config thumb SSIS: Archiving files with VB.Net" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/10/script_task_config_thumb.png" width="570" height="484" /></a> </p>
<p>Click the “Design Script” button and paste the below code into the <a href="http://msdn.microsoft.com/en-us/library/ms974548.aspx" target="_blank">VSA</a> editor.</p>
<pre lang="VB.NET">
' Microsoft SQL Server Integration Services Script Task
' Write scripts using Microsoft Visual Basic
' The ScriptMain class is the entry point of the Script Task.

Imports System
Imports System.Data
Imports System.Math
Imports Microsoft.SqlServer.Dts.Runtime

Public Class ScriptMain

    ' The execution engine calls this method when the task executes.
    ' To access the object model, use the Dts object. Connections, variables, events,
    ' and logging features are available as static members of the Dts class.
    ' Before returning from this method, set the value of Dts.TaskResult to indicate success or failure.
    '
    ' To open Code and Text Editor Help, press F1.
    ' To open Object Browser, press Ctrl+Alt+J.

    Public Sub Main()
        '
        ' Add your code here
        '
        Dim dir As System.IO.Directory
        ' fetch the folder locations from SSIS variables
        Dim inFiles As String = Dts.Variables("inFiles").Value.ToString
        Dim archiveFiles As String = Dts.Variables("archiveFiles").Value.ToString

        ' Get the system date and time separators for removing from the folder name we'll create later
        Dim dateSeparator As String = System.Globalization.DateTimeFormatInfo.CurrentInfo.DateSeparator
        Dim timeSeparator As String = System.Globalization.DateTimeFormatInfo.CurrentInfo.TimeSeparator

        ' Used to create a datetime stamped folder removing separators
        Dim dt As String = DateTime.Now.ToString.Replace(" ", "").Replace(dateSeparator, "").Replace(timeSeparator, "").Replace("/", "")

        ' Check folder paths end with a "\"
        If inFiles.EndsWith("\") = False Then
            inFiles &#038;= "\"
        End If

        If archiveFiles.EndsWith("\") = False Then
            archiveFiles &#038;= "\"
        End If

        archiveFiles &#038;= dt

        ' Create datetime stamp archive folder if it doesn't already exist
        If dir.Exists(archiveFiles) = False Then
            dir.CreateDirectory(archiveFiles)
        End If

        ' Move all files to archive
        For Each archFile As String In dir.GetFiles(inFiles)
            dir.Move(archFile, archiveFiles &#038; "\" &#038; archFile.Substring(archFile.LastIndexOf("\") + 1, archFile.Length - archFile.LastIndexOf("\") - 1))
        Next

        Dts.TaskResult = Dts.Results.Success

    End Sub

End Class
</pre>
<p>Place some files into the location your <strong>inFiles</strong> variable points to. Then right click the script task and choose “Execute Task”. </p>
<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/10/archive_files_with_vb.net_script_task.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="archive_files_with_vb.net_script_task" border="0" alt="archive files with vb.net script task thumb SSIS: Archiving files with VB.Net" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/10/archive_files_with_vb.net_script_task_thumb.png" width="234" height="113" /></a> </p>
<p>Once the package has executed successfully the files will be moved to <strong>archiveFiles</strong> from <strong>inFiles</strong>.</p>
<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/10/archive_folder_with_datetime.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="archive_folder_with_datetime" border="0" alt="archive folder with datetime thumb SSIS: Archiving files with VB.Net" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/10/archive_folder_with_datetime_thumb.png" width="244" height="146" /></a></p>
<p><map name='google_ad_map_402_a45beff5d2e172f6'>
<area shape='rect' href='http://imageads.googleadservices.com/pagead/imgclick/402?pos=0' coords='1,2,367,28' />
<area shape='rect' href='http://services.google.com/feedback/abg' coords='384,10,453,23'/></map>
<img usemap='#google_ad_map_402_a45beff5d2e172f6' border='0' src='http://imageads.googleadservices.com/pagead/ads?format=468x30_aff_img&amp;client=&amp;channel=&amp;output=png&amp;cuid=402&amp;url=http%3A%2F%2Fwww.youdidwhatwithtsql.com%2Fssis-archiving-files-with-vb-net%2F402' title="SSIS: Archiving files with VB.Net" alt=" SSIS: Archiving files with VB.Net" /></p><p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/ssis-archiving-files-with-vb-net/402">SSIS: Archiving files with VB.Net</a></p>
<div class="none"><div class="g-plusone" data-href="http://www.youdidwhatwithtsql.com/ssis-archiving-files-with-vb-net/402" size="standard" count="true"></div></div>]]></content:encoded>
			<wfw:commentRss>http://www.youdidwhatwithtsql.com/ssis-archiving-files-with-vb-net/402/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

