Dynamic SQL Server Integration Services Workflow Based on Variables

By:   |   Updated: 2008-04-29   |   Comments (5)   |   Related: More > Integration Services Control Flow Transformations


Problem

When developing SQL Server Integration Services (SSIS) packages there is sometimes the need to only run certain steps or paths in the package execution either based on time period or maybe a parameter value that is passed to the package or queried from the database. How do you setup an SSIS package to have different execution paths based on a parameter value?

Solution

This is a pretty easy process to setup, but maybe not as intuitive as you might think. There is often the need to have one package do several things, but only having certain steps run at certain times. By setting a variable and then based on this value one of many paths can be taken.

Here is an example of a simple package that just has four Execute SQL Tasks. Each task is just doing a SELECT to illustrate this example, there is nothing unique about this code or this task. The examples below should be able to be used for any of tasks that are available in SSIS.

For this example there are four Execute SQL Tasks with two different paths that can be taken and then control is passed back to Task 3 to complete the package.

s1

If we execute the package we can see that all four tasks execute and complete. This is great if this is what you want to do, but for our example we want to only run Task 2a or Task 2b based on a parameter value.

s2

Here we setup a package variable called "test", but this could be called anything. We are making this a Boolean data type with a value of "False".

s3

If we double click on the workflow arrow from Task 1 to Task 2a we get the dialog box below. Here we are setting the workflow to go down this path based on an Expression value (first dropdown list) and the Expression equals (@test == True). The @test is the name of the variable we setup above. This expression is saying that if our variable "test" equals "True" then to go from Task 1 to Task 2a.

We do the same thing for the workflow arrow from Task 1 to Task 2b, but this is set to (@test == False)

s4

After changing both of these and clicking OK the package should look like the following. You will see the colors of the lines changed from green to blue.

s5

If we execute the package now, we can see that Task 1 and Task 2b run (@test = False), but the package never reaches Task 3.

s6

To fix this problem, we need to double click on the workflow arrow from Task 2a to Task 3 and change the option from "Logical AND" to "Logical OR". With the Logical OR we are saying that either Task 2a or Task 2b needs to complete and then control passes on to Task 3.

s7

Once you click OK the package will look like the following. Even though you only changed one of the workflow values, both changed from "Logical AND" to "Logical OR".

s8

At this point if we execute the package again, we see that the flow goes from Task 1 to Task 2b to Task 3 as planned.

s9

To test our package when our variable value equals "True" we just change our variable from "False" to "True"

s10

If we run it now we can see that since our variable is set to "True" control goes from Task 1 to Task 2a to Task3.

s11

To take this a step further, we will store the value for "test" in a table and query the database to get the value.

First we create a table and add one row to the table with a value of 0 (False).

CREATE TABLE [dbo].[packageControls]( [test] [bit] NULL ) ON [PRIMARY] 
GO

INSERT INTO dbo.packageControls (test) VALUES (0) -- FALSE

For Task 1 we change the query to get the data from table packageControls and change the ResultSet to "Single row" as highlighted below.

s12

On the Result Set window we "Add" a new "Result Name" equal to "test" that maps to our variable "User::test".

s13

At this point our package should execute Task 1 to Task 2b to Task 3.

s14

If we update our table and set "test" equal to "True" we should go from Task 1 to Task 2a to Task 3.

UPDATE [dbo].[packageControls]
SET test = -1 -- TRUE
s15
Next Steps
  • To make your packages more dynamic setup control paths based on variables that can either be set by querying the database or by being passed to the package. Take a look at this tip on how to pass variables to a package
  • Take a look at these other SSIS tips


sql server categories

sql server webinars

subscribe to mssqltips

sql server tutorials

sql server white papers

next tip



About the author
MSSQLTips author Greg Robidoux Greg Robidoux is the President and founder of Edgewood Solutions, a technology services company delivering services and solutions for Microsoft SQL Server. He is also one of the co-founders of MSSQLTips.com. Greg has been working with SQL Server since 1999, has authored numerous database-related articles, and delivered several presentations related to SQL Server. Before SQL Server, he worked on many data platforms such as DB2, Oracle, Sybase, and Informix.

This author pledges the content of this article is based on professional experience and not AI generated.

View all my tips


Article Last Updated: 2008-04-29

Comments For This Article




Wednesday, May 6, 2015 - 7:33:11 AM - Erik Back To Top (37103)

Even in 2015 you're article still continues to help people. Thank you!


Wednesday, June 12, 2013 - 7:19:22 AM - Pallavi Singh Back To Top (25402)

Superb..Thanks a ton!!!


Thursday, February 7, 2013 - 4:39:09 AM - saravana Back To Top (21965)

Fantastic Article . Explaining the concepts clearly . Thanks a lot :)


Tuesday, June 26, 2012 - 9:28:31 PM - Yuki Back To Top (18211)

I really learned a lot from this author.  His articles are the best!


Thursday, March 1, 2012 - 5:42:20 AM - Rajasekhar Back To Top (16225)

super















get free sql tips
agree to terms