<?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>SQL Fool &#187; SSIS</title>
	<atom:link href="http://sqlfool.com/tag/ssis/feed/" rel="self" type="application/rss+xml" />
	<link>http://sqlfool.com</link>
	<description>Adventures in SQL Tuning - a blog for the rest of us</description>
	<lastBuildDate>Wed, 02 Nov 2011 20:39:11 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Getting Started with Variables in SSIS</title>
		<link>http://sqlfool.com/2009/08/getting-started-with-variables-in-ssis/</link>
		<comments>http://sqlfool.com/2009/08/getting-started-with-variables-in-ssis/#comments</comments>
		<pubDate>Tue, 25 Aug 2009 13:13:51 +0000</pubDate>
		<dc:creator>Michelle Ufford</dc:creator>
				<category><![CDATA[Business Intelligence]]></category>
		<category><![CDATA[Syndication]]></category>
		<category><![CDATA[bi]]></category>
		<category><![CDATA[BIDS]]></category>
		<category><![CDATA[SSIS]]></category>

		<guid isPermaLink="false">http://sqlfool.com/?p=1134</guid>
		<description><![CDATA[I recently had to create an SSIS package that used variables to pass data between procs, and I thought it would make a good topic for a blog post. There are many scenarios as to the how and why to use variables in SSIS, but we're going to keep it pretty simple. Let's assume we [...]]]></description>
			<content:encoded><![CDATA[<p>I recently had to create an SSIS package that used variables to pass data between procs, and I thought it would make a good topic for a blog post.  There are many scenarios as to the how and why to use variables in SSIS, but we're going to keep it pretty simple.  Let's assume we have some need to retrieve data from Proc A, pass it to Proc B, and store the results in Table C.  First, let's set up our environment:</p>

<div class="wp_syntax"><div class="code"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">Use</span> AdventureWorks;
Go
&nbsp;
<span style="color: #0000FF;">Create</span> <span style="color: #0000FF;">Procedure</span> dbo.<span style="color: #202020;">LastOrderGet_sp</span>
<span style="color: #0000FF;">As</span>
<span style="color: #0000FF;">Set</span> <span style="color: #0000FF;">NoCount</span> <span style="color: #0000FF;">On</span>;
<span style="color: #0000FF;">Begin</span>
&nbsp;
    <span style="color: #0000FF;">Select</span> <span style="color: #FF00FF;">Max</span><span style="color: #808080;">&#40;</span>SalesOrderID<span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">As</span> <span style="color: #FF0000;">'LastSalesOrderID'</span>
    <span style="color: #0000FF;">From</span> AdventureWorks.<span style="color: #202020;">Sales</span>.<span style="color: #202020;">SalesOrderHeader</span> <span style="color: #0000FF;">With</span> <span style="color: #808080;">&#40;</span>NoLock<span style="color: #808080;">&#41;</span>
&nbsp;
    <span style="color: #0000FF;">Set</span> <span style="color: #0000FF;">NoCount</span> <span style="color: #0000FF;">Off</span>;
    <span style="color: #0000FF;">Return</span> <span style="color: #000;">0</span>;
<span style="color: #0000FF;">End</span>
Go
&nbsp;
<span style="color: #0000FF;">Create</span> <span style="color: #0000FF;">Procedure</span> dbo.<span style="color: #202020;">ProcessLastOrder_sp</span>
    @LastOrderID <span style="color: #0000FF;">int</span>
<span style="color: #0000FF;">As</span>
<span style="color: #0000FF;">Set</span> <span style="color: #0000FF;">NoCount</span> <span style="color: #0000FF;">On</span>;
<span style="color: #0000FF;">Begin</span>
&nbsp;
    <span style="color: #0000FF;">Select</span> SalesOrderDetailID
        , ProductID
        , OrderQty
        , LineTotal
    <span style="color: #0000FF;">From</span> AdventureWorks.<span style="color: #202020;">Sales</span>.<span style="color: #202020;">SalesOrderDetail</span> <span style="color: #0000FF;">With</span> <span style="color: #808080;">&#40;</span>NoLock<span style="color: #808080;">&#41;</span>
    <span style="color: #0000FF;">Where</span> SalesOrderID <span style="color: #808080;">=</span> @LastOrderID;
&nbsp;
    <span style="color: #0000FF;">Set</span> <span style="color: #0000FF;">NoCount</span> <span style="color: #0000FF;">Off</span>;
    <span style="color: #0000FF;">Return</span> <span style="color: #000;">0</span>;
<span style="color: #0000FF;">End</span>
Go
&nbsp;
<span style="color: #0000FF;">Create</span> <span style="color: #0000FF;">Table</span> dbo.<span style="color: #202020;">testStage</span>
<span style="color: #808080;">&#40;</span>
      SalesOrderDetailID    <span style="color: #0000FF;">int</span>
    , ProductId             <span style="color: #0000FF;">int</span>
    , OrderQty              <span style="color: #0000FF;">smallint</span>
    , LineTotal             <span style="color: #0000FF;">numeric</span>
<span style="color: #808080;">&#41;</span>;
Go</pre></div></div>

<p>Now for the fun stuff!  </p>
<p><em>(Please note, I'm assuming some basic understanding of SSIS, so I'm skipping the "how to create a project", etc. stuff and just going to the pertinent parts).</em></p>
<p>Inside BIDS (Business Intelligence Development Studio), create a new SSIS project and call it what you will.  If your Variable window is not already open, open it now by going to <strong>View</strong> --> <strong>Other Windows</strong> --> <strong>Variables</strong>.</p>
<div class="wp-caption alignnone" style="width: 632px"><a href="http://sqlfool.com/blogImages/20090824/SSIS_variable_01.jpg"><img alt="Open Variables Window" src="http://sqlfool.com/blogImages/20090824/SSIS_variable_01.jpg" title="Open Variables Window" width="80%" height="80%" /></a><p class="wp-caption-text">Open Variables Window</p></div>
<p>Now let's create a variable.  To do this, click on the little icon in the upper left-hand corner of the Variables window.  Name the variable <strong>LastSalesOrderID</strong>.</p>
<div class="wp-caption alignnone" style="width: 414px"><a href="http://sqlfool.com/blogImages/20090824/SSIS_variable_02.jpg"><img alt="Create a variable" src="http://sqlfool.com/blogImages/20090824/SSIS_variable_02.jpg" title="Create a variable" width="404" height="203" /></a><p class="wp-caption-text">Create a variable</p></div>
<p>After you create the variable, you should now see it in the Variables window.  Make sure the scope of the variable is the name of your project, which is "Blog" in my case (for obvious reasons); this means the variable is defined at the package scope.  Once you've confirmed that the variable exists, create an Execute SQL task.</p>
<p><em>(Variables in SSIS, like in other programming languages, can have different scopes.  For instance, a package scope means the variable can be accessed anywhere within the package, but a variable with a Data Flow scope can only be accessed within the specified Data Flow task.)</em></p>
<div class="wp-caption alignnone" style="width: 646px"><a href="http://sqlfool.com/blogImages/20090824/SSIS_variable_03.jpg"><img alt="Create Execute SQL Task" src="http://sqlfool.com/blogImages/20090824/SSIS_variable_03.jpg" title="Create Execute SQL Task" width="80%" height="80%" /></a><p class="wp-caption-text">Create Execute SQL Task</p></div>
<p>Double-click on your Execute SQL Task and configure with the following values:</p>
<ul>
<li>Set "Result Set" to <strong>Single Row</strong>.</li>
<li>Set your Connection to your appropriate data source.</li>
<li>Set your SQL Statement to: <strong>Execute AdventureWorks.dbo.LastOrderGet_sp;</strong></li>
</ul>
<div class="wp-caption alignnone" style="width: 635px"><a href="http://sqlfool.com/blogImages/20090824/SSIS_variable_04.jpg"><img alt="Set up your Execute SQL Task" src="http://sqlfool.com/blogImages/20090824/SSIS_variable_04.jpg" title="Set up your Execute SQL Task" width="80%" height="80%" /></a><p class="wp-caption-text">Set up your Execute SQL Task</p></div>
<p>Now click on "Result Set" and click on "Add."  You'll want to put the name of the column that's returned by the proc in the "Result Name" column; in our case, that'll be <strong>LastSalesOrderID</strong>.  Click on the Variable Name column and scroll down until you find the appropriate one (<strong>User::LastSalesOrderID</strong>).</p>
<div class="wp-caption alignnone" style="width: 592px"><a href="http://sqlfool.com/blogImages/20090824/SSIS_variable_05.jpg"><img alt="Mapping the results to a variable" src="http://sqlfool.com/blogImages/20090824/SSIS_variable_05.jpg" title="Mapping the results to a variable" width="582" height="563" /></a><p class="wp-caption-text">Mapping the results to a variable</p></div>
<p>Go ahead and add a Data Flow task to the designer surface.  We don't need to use a Data Flow task here -- for example, we could use another Execute SQL task instead -- but this will help demonstrate one way to use variables.  </p>
<div class="wp-caption alignnone" style="width: 646px"><a href="http://sqlfool.com/blogImages/20090824/SSIS_variable_06.jpg"><img alt="Add Data Flow Task" src="http://sqlfool.com/blogImages/20090824/SSIS_variable_06.jpg" title="Add Data Flow Task" width="80%" height="80%" /></a><p class="wp-caption-text">Add Data Flow Task</p></div>
<p>Double-click on the Data Flow task and add an OLE DB Source, then double-click on it to open up the properties.  Enter the following text in the "SQL Command text" window:<br />
<strong>Execute AdventureWorks.dbo.ProcessLastOrder_sp ?</strong><br />
The question mark (?) tells SSIS to expect a parameter.  </p>
<div class="wp-caption alignnone" style="width: 660px"><a href="http://sqlfool.com/blogImages/20090824/SSIS_variable_07.jpg"><img alt="Edit OLE DB Source" src="http://sqlfool.com/blogImages/20090824/SSIS_variable_07.jpg" title="Edit OLE DB Source" width="80%" height="80%" /></a><p class="wp-caption-text">Edit OLE DB Source</p></div>
<p>Now click on the Parameters button on the left.  This is where we map our variable to our parameter.  For the "Parameters" value, enter <strong>@LastOrderID</strong> (the parameter the stored procedure is expecting).  In the "Variables" column, click on the drop-down and navigate to the <strong>User::LastSalesOrderID</strong> variable. </p>
<div class="wp-caption alignnone" style="width: 406px"><a href="http://sqlfool.com/blogImages/20090824/SSIS_variable_08.jpg"><img alt="Map Variables to Parameters" src="http://sqlfool.com/blogImages/20090824/SSIS_variable_08.jpg" title="Map Variables to Parameters" width="396" height="411" /></a><p class="wp-caption-text">Map Variables to Parameters</p></div>
<p>Finally, set up an OLE DB Destination, and configure the OLE DB Source to load into the testStage table.  </p>
<div class="wp-caption alignnone" style="width: 660px"><a href="http://sqlfool.com/blogImages/20090824/SSIS_variable_09.jpg"><img alt="Configure OLE DB Destination" src="http://sqlfool.com/blogImages/20090824/SSIS_variable_09.jpg" title="Configure OLE DB Destination" width="80%" height="80%" /></a><p class="wp-caption-text">Configure OLE DB Destination</p></div>
<p>At this point, you should be able to successfully execute your package.  Upon successful execution, the testStage table will return the following results:</p>

<div class="wp_syntax"><div class="code"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">Select</span> <span style="color: #808080;">*</span> <span style="color: #0000FF;">From</span> testStage;
&nbsp;
SalesOrderDetailID ProductId   OrderQty LineTotal
<span style="color: #008080;">------------------ ----------- -------- ------------------</span>
<span style="color: #000;">121315</span>             <span style="color: #000;">878</span>         <span style="color: #000;">1</span>        <span style="color: #000;">21</span>
<span style="color: #000;">121316</span>             <span style="color: #000;">879</span>         <span style="color: #000;">1</span>        <span style="color: #000;">159</span>
<span style="color: #000;">121317</span>             <span style="color: #000;">712</span>         <span style="color: #000;">1</span>        <span style="color: #000;">8</span></pre></div></div>

<p>That's all for now.  Hopefully this gives you an idea of how easy and useful it is to work with variables in SSIS.</p>
]]></content:encoded>
			<wfw:commentRss>http://sqlfool.com/2009/08/getting-started-with-variables-in-ssis/feed/</wfw:commentRss>
		<slash:comments>19</slash:comments>
		</item>
	</channel>
</rss>

