Russ' Do It Yourself Home Workshop

Finding Fixes to Just About Anything and Everything

Setting SSIS Variables the SQL Way: Create a Directory or Folder using Dates

Posted by Russell Wright on January 16, 2014

Okay, I’m not an SSIS expert, but I do use it quite a bit.  So, when I had to do some “coding” in the script task recently I was able to get through it, but the next time I had to do something I wanted to determine an alternate, and possibly more expedient, way of accomplishing this simple task:  Set a variable based on dates or other easily generated data.

My task was pretty simple.  Create a folder structure during the execution of an SSIS package in which the files received during the B2B process can be placed.  It’s been done a million times, but there’s always a new and different way to accomplish this simple feat.  And, with SSIS, there are always intricacies of different data connection types and other obtuse settings that are not necessarily obvious to the uninitiated.

My inspiration came from The Data Queen and this blog post, “How to Set and Use Variables in SSIS Execute SQL Task.”  Basically what you are doing here is selecting a single row results set using the Execute SQL task and setting (initializing) your variable to one of the columns returned from the query. 


This gives you a pretty flexible approach where you can create a name/value pair table, such as this, and select a value from the table with which you initialize your SSIS variable.

[Name] [varchar](255) NOT NULL,
[Value] [varchar](255) NOT NULL,
[Description] [varchar](255) NULL

But what I wanted to do wasn’t really about storing values to retrieve (well it was this too, but that’s not really what I’m talking about right now), but instead calculate values in order to create a dynamic folder name each time the package is executed.  And, I wanted to use regular SQL to do this, if possible.  Here’s what I found.

We will be using the Execute SQL Task and the File System Task.  I’ve named them Set Archive Folder Variable OLEDB and Create Archive Folder.  The reason I added OLEDB in the name is that, in this case, we are using the OLEDB data connection type, as opposed to the ADO.NET connection type.  More on that later.


User variables are created and scoped at the package level.  This process is not very intuitive if you’ve never done it before or, if you’re like me, you do it once-in-a-blue-moon.

You first have to make sure the Variables pane is enabled.  If you get a short menu, that means your focus is not on the SSIS designer canvas, as the menus change based on your current context (this is soooo OS/2-ish). 

image  image

You should now have a Variables tab along with a Toolbox tab.  The icons along the top are pretty self-explanatory if you hover over each one:  Create a variable, delete a variable, show system variables, show all variables, and add/remove columns to the menu display.  Here you can see my two variables, TRAXRootFolder and ArchiveFolder.  My package name is Test, so the scope is at the package level.  Both are String data types.


Let’s look at the settings of the Execute SQL Task.  I’ve highlighted the important areas and will discuss each one.


The SQLStatement will show you very quickly what I am attempting to do.  I am using SQL to generate a result set without an underlying table query. 


Declare @ArchiveFolder varchar(255) — Create SQL variable in which to hold results
Select @ArchiveFolder = ? + CONVERT(VARCHAR, GETDATE(), 112) — Concatenates the root folder variable passed in (F:\TRAXData\B2B\) with a generated date
Select @ArchiveFolder AS Value — Selects the value in the SQL variable and returns it in the result set with a column name of “Value”


The ResultSet property should be set to Single row.  If you set it to something other than Single row you’ll get an error.

The ConnectionType is very important.  If you’ve dealt with SSIS, you know that passing parameters depends upon the type of connection you have.  This TechNet article provides the following references to parameter markers and parameters names.  In this case, using OLEDB, we use the question mark (?) as the parameter marker or placeholder and the parameter names are ordinals with a zero base (0, 1, 2, 3…).  I’ll show you what this looks like if you use the ADO.NET connection at the end of this blog.



Parameter Mapping


Result Set














Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: