<?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>www.copyandwaste.com &#187; SSIS</title>
	<atom:link href="http://www.copyandwaste.com/category/ssis/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.copyandwaste.com</link>
	<description></description>
	<lastBuildDate>Tue, 17 Aug 2010 20:53:32 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.6</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>SSIS Expressions are great</title>
		<link>http://www.copyandwaste.com/2007/12/17/ssis-expressions-are-great/</link>
		<comments>http://www.copyandwaste.com/2007/12/17/ssis-expressions-are-great/#comments</comments>
		<pubDate>Tue, 18 Dec 2007 03:28:19 +0000</pubDate>
		<dc:creator>Andrew Konkol</dc:creator>
				<category><![CDATA[SSIS]]></category>

		<guid isPermaLink="false">http://copyandwaste.com/archive/2007/12/17/ssis-expressions-are-great.aspx</guid>
		<description><![CDATA[Previously I posted about how to obtain relative picture paths from your database, flip the direction of the &#8220;slashes&#8221; and delete the image using a &#8216;Filesystem Task.&#8217;  I posted a stored procedure that does a &#8216;replace&#8217; on forward slashes with backslashes, and I don&#8217;t think that is the best way to do it.  The database [...]]]></description>
			<content:encoded><![CDATA[<p>Previously I posted about how to obtain relative picture paths from your database, flip the direction of the &#8220;slashes&#8221; and delete the image using a &#8216;Filesystem Task.&#8217;  I posted a stored procedure that does a &#8216;replace&#8217; on forward slashes with backslashes, and I don&#8217;t think that is the best way to do it.  The database should have the least amount of work to do period.  Expressions are a way to evaluate and calculate certain values on the fly.  That being said, use expressions.</p>
<p><strong>Example Scenario:</strong>  You have a stored proc that returns a result set of relative paths (i.e. /images/yourface.jpg&#8221;).  You want to turn that path into the full UNC path.</p>
<p> </p>
<p><strong>Step 1: </strong>Create a variable to store the returned path</p>
<ul>
<li>In the variables pane click Add Variable (call it ReturnedPath) as a String</li>
<li>Create an other variable called BaseDir as a String and hard code the value for your base directory (i.e. <a href="file://\\fileserver\uploads">\\fileserver\uploads</a>)</li>
<li>Create an other variable called FullPath as a String</li>
</ul>
<p><strong>Step 2:</strong> Create an expression for the FullPath variable</p>
<ul>
<li>Click on the variable FullPath and select EvaluateAsExpression to True</li>
<li>Click and expand the Expression Property </li>
<li>Expression should be: @[User::BaseDir] +   REPLACE( @[User::ReturnedPath] ,&#8221;/&#8221;, &#8220;\\&#8221; )</li>
</ul>
<p>What this does:  It takes the hard coded value of BaseDir and concatenates the ReturnedPath with the forward slashes replaced with backslashes</p>
<p><a href="http://copyandwaste.com/images/copyandwaste_com/WindowsLiveWriter/SSISExpressionsaregreat_E7AA/SSIS_Expressions_pic1.jpg"><img width="600" height="321" border="0" style="border: 0px none ;" alt="SSIS_Expressions_pic1" src="http://copyandwaste.com/images/copyandwaste_com/WindowsLiveWriter/SSISExpressionsaregreat_E7AA/SSIS_Expressions_pic1_thumb.jpg" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.copyandwaste.com/2007/12/17/ssis-expressions-are-great/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Deleting Files with SSIS</title>
		<link>http://www.copyandwaste.com/2007/12/13/deleting-files-with-ssis/</link>
		<comments>http://www.copyandwaste.com/2007/12/13/deleting-files-with-ssis/#comments</comments>
		<pubDate>Thu, 13 Dec 2007 07:07:54 +0000</pubDate>
		<dc:creator>Andrew Konkol</dc:creator>
				<category><![CDATA[SSIS]]></category>

		<guid isPermaLink="false">http://copyandwaste.com/archive/2007/12/13/deleting-files-with-ssis.aspx</guid>
		<description><![CDATA[A common practice is to have user&#8217;s upload images to a directory that is readable from IIS, store that upload path to a database, and then construct a relative path to that upload directory for use within your web application.  What happens if that data has to be removed?
Example Scenario: You are running your own [...]]]></description>
			<content:encoded><![CDATA[<p>A common practice is to have user&#8217;s upload images to a directory that is readable from IIS, store that upload path to a database, and then construct a relative path to that upload directory for use within your web application.  What happens if that data has to be removed?</p>
<p><strong>Example Scenario:</strong> You are running your own version of a social networking web application which users can upload photos of themselves and save it in a profile.  That user wishes to remove themselves from your social networking site and you must delete all database records pertaining to them as well as all the pictures (binaries) that they have uploaded.</p>
<p><strong>Possible Solutions:</strong></p>
<ol>
<li>Pull the full path for all the binaries from your database and create a batch/vb script to delete the picture files. </li>
<li>Leave the images there, because after the record containing that image&#8217;s path is deleted it will never be used (bad idea, you would be eating up un-necessary space). </li>
<li>Create a SSIS package to pull the relative path from the database, construct a UNC or local path and delete the picture files </li>
</ol>
<p> </p>
<h3>Creating an SSIS package to remove files:</h3>
<p><strong>Step 1: Creating the stored procedure</strong> </p>
<p>You need to create a stored procedure to retrieve all of the records that contain some sort of path to where the image is stored.  Quite frequently this path is inserted into the database table as a path that is relative to your IIS instance.  The stored procedure below allows you to supply a user id (named @pUserID) and returns all records in the UserPictures table for that particular user id. Notice that I am flipping the direction of slashes, why do you ask?  Since relative paths are commonly stored for image paths they look like /images/someotherfolder/somephoto.jpg, if we are to delete pictures that are on the local path or UNC path we need to flip the direction of the slashes.</p>
<div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 35.92%; cursor: text; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, 'Courier New', courier, monospace; height: 208px; background-color: #f4f4f4; max-height: 200px">
<div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #606060">   1:</span> <span style="color: #0000ff">CREATE</span> <span style="color: #0000ff">Procedure</span> GetImagesByUserID</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">   2:</span> @pUserID <span style="color: #0000ff">int</span></pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #606060">   3:</span>  </pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">   4:</span> <span style="color: #0000ff">AS</span></pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #606060">   5:</span> <span style="color: #0000ff">BEGIN</span></pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">   6:</span>  </pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #606060">   7:</span> <span style="color: #0000ff">SELECT</span> replace(RelativePath,<span style="color: #006080">'/'</span>,<span style="color: #006080">'\'</span>)</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">   8:</span> <span style="color: #0000ff">FROM</span> UserPictures</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #606060">   9:</span> <span style="color: #0000ff">WHERE</span> UserID=@pUserID</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">  10:</span> END</pre>
</p></div>
</div>
<p>
  <br /><strong>Step 2: Create a new SSIS package with variables</strong> </p>
<p>Although you should keep the number of variables in your package to a minimum, create the following variables:</p>
<p><em>UserID</em> has the value of &#8216;911&#8242; as a piece of sample data you will pass into the @pUserID later on</p>
<p><em>ImageRecords</em> is of data type &#8216;Object&#8217; because it will hold the resulting record set from the stored proc created in Step 1.</p>
<p> <a href="http://copyandwaste.com/images/copyandwaste_com/WindowsLiveWriter/DeletingFileswithSSIS_1338F/SSIS_FS_ImageDeletion_step2_6.gif"><img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="216" alt="SSIS_FS_ImageDeletion_step2" src="http://copyandwaste.com/images/copyandwaste_com/WindowsLiveWriter/DeletingFileswithSSIS_1338F/SSIS_FS_ImageDeletion_step2_thumb_2.gif" width="428" border="0" /></a> </p>
<p><strong>Step 3: Retrieve image paths using &#8216;Execute SQL Task&#8221;</strong> </p>
<p>This task will supply the <em>UserID</em> variable to our stored proc and execute it.  </p>
<p> <a href="http://copyandwaste.com/images/copyandwaste_com/WindowsLiveWriter/DeletingFileswithSSIS_1338F/SSIS_FS_ImageDeletion_step3a_4.gif"><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="377" alt="SSIS_FS_ImageDeletion_step3a" src="http://copyandwaste.com/images/copyandwaste_com/WindowsLiveWriter/DeletingFileswithSSIS_1338F/SSIS_FS_ImageDeletion_step3a_thumb_1.gif" width="604" border="0" /></a> </p>
<p>Configure the &#8216;Execute SQL Task&#8217; to:</p>
<ul>
<li>Return a Full Result Set </li>
<li>Use ADO.NET as the ConnectionType </li>
<li>Select your connection </li>
<li>Write the SQL statement to execute the stored proc </li>
</ul>
<p> <a href="http://copyandwaste.com/images/copyandwaste_com/WindowsLiveWriter/DeletingFileswithSSIS_1338F/SSIS_FS_ImageDeletion_step3b_4.gif"><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="479" alt="SSIS_FS_ImageDeletion_step3b" src="http://copyandwaste.com/images/copyandwaste_com/WindowsLiveWriter/DeletingFileswithSSIS_1338F/SSIS_FS_ImageDeletion_step3b_thumb_1.gif" width="604" border="0" /></a> </p>
<p>Click on the Parameter Mapping tab and click &#8220;Add&#8221;</p>
<p>Configure:</p>
<ul>
<li>Variable Name is <em>User::UserID</em></li>
<li>Direction is Input</li>
<li>Data Type is Int32</li>
<li>Parameter Name is @pUserID</li>
<li>Parameter Size does not matter</li>
</ul>
<p><a href="http://copyandwaste.com/images/copyandwaste_com/WindowsLiveWriter/DeletingFileswithSSIS_1338F/SSIS_FS_ImageDeletion_step3ab_2.gif"><img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="561" alt="SSIS_FS_ImageDeletion_step3ab" src="http://copyandwaste.com/images/copyandwaste_com/WindowsLiveWriter/DeletingFileswithSSIS_1338F/SSIS_FS_ImageDeletion_step3ab_thumb.gif" width="588" border="0" /></a> </p>
<p>Click on the Result Set section and click &#8220;Add&#8221; and set the Result Name to &#8216;0&#8242; and the Variable Name to <em>User::ImageRecords</em>, this will assign the output from the stored proc to your <em>ImageRecords</em> variable. </p>
<p> <a href="http://copyandwaste.com/images/copyandwaste_com/WindowsLiveWriter/DeletingFileswithSSIS_1338F/SSIS_FS_ImageDeletion_step3c_4.gif"><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="482" alt="SSIS_FS_ImageDeletion_step3c" src="http://copyandwaste.com/images/copyandwaste_com/WindowsLiveWriter/DeletingFileswithSSIS_1338F/SSIS_FS_ImageDeletion_step3c_thumb_1.gif" width="604" border="0" /></a> </p>
<p>
  <br /><strong>Step 4: Loop through each record returned using a &#8216;Foreach Loop Container&#8217;</strong> </p>
<p>Use a Foreach Loop Container to iterate through the resulting record set from the step above. *Its not generally a good idea to iterate through every record of a record set (especially with thousands of records), but in this case it is very useful.</p>
<p><a href="http://copyandwaste.com/images/copyandwaste_com/WindowsLiveWriter/DeletingFileswithSSIS_1338F/SSIS_FS_ImageDeletion_step4_2.gif"><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="371" alt="SSIS_FS_ImageDeletion_step4" src="http://copyandwaste.com/images/copyandwaste_com/WindowsLiveWriter/DeletingFileswithSSIS_1338F/SSIS_FS_ImageDeletion_step4_thumb.gif" width="604" border="0" /></a> </p>
<p>Click on the newly created Foreach Loop Container and create a new variable called <em>CurrentFile</em>, this will provide a variable for the &#8216;Foreach Loop Container&#8217; to assign the image path to.</p>
<ul>
<li><em>CurrentFIle</em> is a variable for the &#8216;Foreach Loop Container&#8217; to assign the image path to</li>
<li><em>BaseDir</em> is a variable to which you assign a UNC or local path name (<a href="file://\\myfileserver\mywebapp\">\\myfileserver\mywebapp\</a>, C:\Data)</li>
<li><em>FullPath</em> is a variable which builds the full path for the image </li>
</ul>
<p><a href="http://copyandwaste.com/images/copyandwaste_com/WindowsLiveWriter/DeletingFileswithSSIS_1338F/SSIS_FS_ImageDeletion_step4a_4.gif"><img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="298" alt="SSIS_FS_ImageDeletion_step4a" src="http://copyandwaste.com/images/copyandwaste_com/WindowsLiveWriter/DeletingFileswithSSIS_1338F/SSIS_FS_ImageDeletion_step4a_thumb_1.gif" width="638" border="0" /></a> </p>
<p>Click on the <em>FullPath</em> variable and go to the Properties window</p>
<p>Configure:</p>
<ul>
<li>EvaulateAsExpression is True</li>
<li>Expression is @[User::BaseDir] + @[User::CurrentFile]</li>
</ul>
<p><a href="http://copyandwaste.com/images/copyandwaste_com/WindowsLiveWriter/DeletingFileswithSSIS_1338F/SSIS_FS_ImageDeletion_step4ab_2.gif"><img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="447" alt="SSIS_FS_ImageDeletion_step4ab" src="http://copyandwaste.com/images/copyandwaste_com/WindowsLiveWriter/DeletingFileswithSSIS_1338F/SSIS_FS_ImageDeletion_step4ab_thumb.gif" width="498" border="0" /></a> </p>
<p>Double click on the &#8216;Foreach Loop Container&#8217; and click on the Collection tab and configure:</p>
<ul>
<li>Enumerator is a Foreach ADO Enumerator </li>
<li>ADO object source variable is <em>User::ImageRecords</em> this will tell the loop to iterate through the records in the <em>ImageRecords</em> variable. </li>
</ul>
<p><a href="http://copyandwaste.com/images/copyandwaste_com/WindowsLiveWriter/DeletingFileswithSSIS_1338F/SSIS_FS_ImageDeletion_step4b_2.gif"><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="560" alt="SSIS_FS_ImageDeletion_step4b" src="http://copyandwaste.com/images/copyandwaste_com/WindowsLiveWriter/DeletingFileswithSSIS_1338F/SSIS_FS_ImageDeletion_step4b_thumb.gif" width="584" border="0" /></a> </p>
<p>Click the Variable Mappings tab and configure:</p>
<ul>
<li>Variable to <em>&#8220;User::CurrentFile&#8221;</em> </li>
<li>Index to &#8216;0&#8242; </li>
</ul>
<p>This assigns the value of cell &#8216;0&#8242; of the current record to <em>CurrentFile</em></p>
<p><a href="http://copyandwaste.com/images/copyandwaste_com/WindowsLiveWriter/DeletingFileswithSSIS_1338F/SSIS_FS_ImageDeletion_step4c_2.gif"><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="561" alt="SSIS_FS_ImageDeletion_step4c" src="http://copyandwaste.com/images/copyandwaste_com/WindowsLiveWriter/DeletingFileswithSSIS_1338F/SSIS_FS_ImageDeletion_step4c_thumb.gif" width="586" border="0" /></a> </p>
<p>
  <br /><strong>Step 5: Delete the file using File System Task</strong></p>
<p>Drag a new &#8216;File System Task&#8217; into your Foreach Loop Container and double click it.</p>
<p><a href="http://copyandwaste.com/images/copyandwaste_com/WindowsLiveWriter/DeletingFileswithSSIS_1338F/SSIS_FS_ImageDeletion_step5a_4.gif"><img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="440" alt="SSIS_FS_ImageDeletion_step5a" src="http://copyandwaste.com/images/copyandwaste_com/WindowsLiveWriter/DeletingFileswithSSIS_1338F/SSIS_FS_ImageDeletion_step5a_thumb_1.gif" width="615" border="0" /></a> </p>
<p>Configure:</p>
<ul>
<li>Operation is Delete File</li>
<li>IsSourcePathVariable is True</li>
<li>SourceVariable is <em>User::FullPath</em></li>
</ul>
<p><a href="http://copyandwaste.com/images/copyandwaste_com/WindowsLiveWriter/DeletingFileswithSSIS_1338F/SSIS_FS_ImageDeletion_step5b_4.gif"><img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="562" alt="SSIS_FS_ImageDeletion_step5b" src="http://copyandwaste.com/images/copyandwaste_com/WindowsLiveWriter/DeletingFileswithSSIS_1338F/SSIS_FS_ImageDeletion_step5b_thumb_1.gif" width="586" border="0" /></a>  </p>
<p>Your final package should look something like this:</p>
<p><a href="http://copyandwaste.com/images/copyandwaste_com/WindowsLiveWriter/DeletingFileswithSSIS_1338F/SSIS_FS_ImageDeletion_step5c_2.gif"><img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="441" alt="SSIS_FS_ImageDeletion_step5c" src="http://copyandwaste.com/images/copyandwaste_com/WindowsLiveWriter/DeletingFileswithSSIS_1338F/SSIS_FS_ImageDeletion_step5c_thumb.gif" width="621" border="0" /></a> </p>
<p>
  <br /><strong>*Notes:</strong> SSIS package must be run by a user that has permissions to file.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.copyandwaste.com/2007/12/13/deleting-files-with-ssis/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
