The Logic of IF...THEN
Need the simplicity of the IF...THEN statement in your SSIS packages? It can be done.
- By Eric Johnson
One of the questions I am regularly asked is how to build IF...THEN logic into an SSIS package. While there are no built-in tasks or transformations that provide you an IF...THEN style comparison, there are a couple ways to make this happen. If you need this logic in your Control Flow, you can use Precedence Constraints and if you need it in your data flow, you use Conditional Split.
For Control Flow, the easiest thing to do is make your logical comparison in a script or with an expression and then set a variable value to use as an answer flag. For example, say you want to check for the existence of a particular file on disk. You can do that via a script task and then set an SSIS variable to True or False to indicate whether there are files. Then you can just edit the Precedence Constraint following the script to use an expression that checks your variable value (see Fig. 1). You can build as many paths after the script as you need. This allows you to mimic not only IF...THEN logic but also CASE logic.
|Figure 1. To mimic an IF...THEN in the SSIS package, edit the Precedence Constraint that follows the script. (Click image to view larger version.)
Inside a Data Flow, the easiest thing to do is to use the Conditional Split transformation. I won't go into a lot of detail, but this transformation allows you to evaluate the data in your package and split it up based on values. In other words, you are able to send data down multiple paths based on the values in the data. Alternatively, you could write a custom transformation script if you feel you need something more complex.
While there are no tasks or transformation explicitly called IF...THEN, you can functionally create the same logic using the tips in this post. I hope this makes your SSIS package development just a little easier.
Eric Johnson, SQL Server MVP, is the owner of Consortio Services in Colorado Springs providing IT systems management and technology
consulting. He is also the President of the Colorado Springs SQL Server
User Group. He can be contacted at www.consortioservices.com.