31 Days of SSIS – Environmental Variable(17/31)

It’s time to go back to discussing the environment when it comes to SSIS packages.  For the last few posts of the 31 Days of SSIS series I’ve talked about developing packages.  As important as developing packages is, we must be prepared to develop them in a way that they can be securely deployed.

As part of this need, this post will focus on the use of environmental variables with SSIS packages.  Environmental variables allow variables to be specified and set within an operating system environment.  The value is available to all applications and exists from one reboot to another.  A very common example of an environmental variable is TEMP variable which directs the operating system to use C:WindowTemp.  At least, that is where it is set on my Windows 7 laptop.

Why Environmental Variable?

Since environmental variables are specific to an environment, they can be use to store information specific to that environment.  In this context of the term “environment”, I am referring to your development, test, and production environments.  Each of these will have some statics values that are dependent on the environment.

When looking at these environments, there are some common factors that all SSIS packages will have regardless of the purpose of the package.  For instance, all of the SSIS packages that reference the AdventureWorks database in the development environment will use one server.  All of those same SSIS packages will reference a different server or instance when they are executed in the test or production environment.  These settings that are specific to your environments are the configuration values of SSIS packages that are prime candidates to use with environmental variables

When environmental variables are used properly with SSIS packages, they can be deployed to any environment and perform within the confines of that environment.  This when an SSIS package is developed on my workstation, the local environmental variable can reference the development servers.  When I deploy it to the test environment, the testers can execute the package and it will automatically reference the test environment.  Once they validate the package, it can be deployed to production and the changes for that environment will just occur.

Environmental Variable Setup

Let’s suppose, as mentioned above, we have a number of SSIS packages that will be referencing the AdventureWorks database.  We will want the packages that run on my laptop to point to a local instance.  When executed on other servers we will need to point to other instances or servers.

ssis-114In order to accomplish this we need an environmental variable and to configure it in an SSIS package.  To start this setup, open the System Properties window for the desktop or server that needs to have the environmental variable created.  This can be found in Computer –> Properties –> Advanced system settings.  From this window choose the Environmental Variables button.

ssis-115From the Environmental Variable window select the New… button.  This will open a dialog box where you are able to type in the name of the variable and it’s value.  For this demonstration, name the variable AdventureWorks.  That value should be – Data Source = STRATE-02SQL2008R2; Initial Catalog = AdventureWorks; Provider = SQLNCLI10.1; Integrated Security = SSPI; Application Name = EnvironmentalVariable;.

ssis-116Of course, that connection string will be modified slightly to reference your own SQL Server.  I’m pretty certain my server is locked down enough to prevent you from gaining access to it.

After this is done, the environmental variable will be available in the list of variables for the environment.  If you used the variable named above, it will appear at the top of the list in the Environmental Variables window.  ssis-117Unfortunately, there is one more task that needs to be completed to finish setting this up.  You desktop, laptop, or server will need to be restarted.  Until then the variable will be there but it won’t be accessible to SSIS.  Forgetting this can be a pain so I will reiterate – reboot after you setup the variable.

Now that you have a variable to work with, open up an SSIS package.  Add a connection to AdventureWorks in Connection Managers.  From here we are ready to begin.

ssis-118Open the Package Configuration Organizer window. This can be accessed through the menu at   or by right-clicking in the Control Flow.  In this window select Enable package configurations and click the Add… button.

In the Package Configuration wizard, choose the Configuration type of Environmental variable.  The drop-down box will list the AdventureWorks environmental variable along with all other environment variable on the system.  Select this item and click Next.

ssis-119On the next screen, you need to associate the environmental variable with a property in the SSIS package.  The property we will want is Connection Managers –> AdventureWorks –> Properties –> ConnectionString.  Browse to this item and then select Next.

The last screen will allow you to name the configuration setting.  It can be named anything that you’d like.  Often I choose to name the configuration after the environmental variable variable for clarity.

Once you click on Finish, the setup of the environmental variable will be complete.  All you need to do now is set this up in your other environments and drop the package in and run it.  As long as the connection string is pointed to the correct server, this and all other similarly configured packages will have one place that controls how they execute in each environment.

Environmental Variable Wrap-Up

I find using environmental variables for directing connectivity of SSIS packages to be one of the more perfect uses for it.  As long as everyone is on the same page, a lot of the work in deploying packages is removed and you can focus on the features of the SSIS packages rather than the functionality.

The other benefit, as mentioned in the last section, is consolidation of configuration information.  Rather than having 5, 10, 20, or more packages to edit when a server moves or information changes – the environmental variable allows for a single point of configuration for the entire environment.  One edit and everything starts working with the new configurations.

Package Download

17of31-EnvironmentVariables

19 thoughts on “31 Days of SSIS – Environmental Variable(17/31)

  1. When you deploy this in production and create the system environment variable in that server. Does it requires to be restarted?

    Like

    1. If you are remoting into a server it is needed. On your local machine you just need to restart Visual Studio.

      Like

  2. Dammit, I didn't read the next article (describing indirect configurations) before I posted my comment above. Just ignore it 🙂

    Like

  3. Another great article. I tend to use environment variables just to store a connection string to a SQL Server configuration database (aka indirect package configuration). In this database I have my configuration tables (one for each project). As a package moves from one environment to another, it knows immediately where to find its configurations.

    Like

  4. Humm..it seems nice. This is nice and i will be using and applying it.But all i want to now about detail's working behind of this and dtsconfig file.

    Like

    1. Check out the posts SQL Server Configuration (18/31) and DTSConfig Configuration (19/31), they pick up where this post left off.

      A quick summary… Environmental variables should be used for long term, seldom changed environment properties. SQL Server configurations for environment properties that may change or will be added to the environment from time to time and might not be related directly to a release. DTSConfig (XML) for properties that need to change during each execution of an SSIS package.

      Like

  5. Humm..it seems nice. This is nice and i will be using and applying it.But all i want to now what's the different between this and dtsconfig file.

    Like

Comments are closed.