SSIS: execute first task if condition met else skip to next

后端 未结 7 1944
忘了有多久
忘了有多久 2021-01-11 17:46

I am getting to know SSIS, I apologize if the question is too simple.

I got a set of tasks inside a foreach-loop-container.
T

相关标签:
7条回答
  • 2021-01-11 17:50

    Issue 1: There are two ways to interpret your logic: "...a certain user variable is not null or empty":

    1. The (Variable is Not Null) OR the (Variable is Empty).
    2. The (Variable is Not Null) OR the (Variable is Not Empty).

      It's all about the object(s?) of the word "not". The differences are subtle but will impact when the first task in the Foreach loop executes. For demonstration purposes, I am assuming you intend #1.

      Issue 2: The first task can no longer be first. In order to accomplish what you desire using SSIS inside the BIDS environment, you need to place another task ahead of the task formerly known as "the first task". This is so you can set a Precedence Constraint on the former first task from the new first task. It is possible to accomplish what you desire by designing your SSIS dynamically from managed code, but I don't think this issue warrants the overhead associated with that design choice. I like using an empty Sequence Container as an "Anchor" task - a task that exists solely to serve as the starting endpoint of a Precedence Constraint. I heavily document them as such. I don't want anyone deleting the "unnecessary empty container" and roaming the halls for days shaking their heads and repeating "Andy, Andy, Andy..." but I digress.

      In the example below, I have two precedence constraints leaving the empty Sequence Container. One goes to the task that may be skipped and the other to the task following the task that can sometimes be skipped. A third precedence constraint is required between the task that can sometimes be skipped and the task following. It is important to note this third precedence constraint must be edited and the Multiple Constraints option set to OR. This allows the task following to execute when either of the mutually exclusive previous paths are taken. By default, this is set to AND and will require both paths to execute. By definition, that will not - cannot - happen with mutually exclusive paths.

      I test the value of an SSIS String variable named @MyVar to see if it's Null or Empty. I used the Expression Only Evaluation Option for the constraints leaving the empty Sequence Container. The expressions vary but establish the mutual exclusivity of the expression. My Foreach Loop Container looks like this:

    I hope this helps.

    :{>

    0 讨论(0)
  • 2021-01-11 17:52

    Hopefully I didn't misunderstand the question but a possible solution can be as written below.

    I created a sample ForEach loop. The loop itself is an item enumerator. It enumerates the numbers 1, 2, 3. The acutal value is stored in a variable called LoopVariable.

    Sample Foreach loop

    Settings of the loop

    Assigning to variable

    There is another variable named FirstShouldRun which is a Boolean variable showing the first task in the foreach loop should be runned or not. I set this variable's EvaluateAsExpression property to true, and its expression is (@[User::LoopVariable] % 2) == 0. I would like to demonstrate with this that every second time the first task should be started.

    Variables

    Setting of the property of the variable

    The two tasks do nothing much but display a MessageBox showing the task has been started.

    I started the package and first and the third time the first task didn't started. In the second loop the MessageBox (showing "First started") appeared.

    After that you should set FirstShouldRun variable as you like.

    As I mentioned in my first comment to the OP, this solution is based on the idea of Amos Wood written in another answer.

    0 讨论(0)
  • 2021-01-11 17:56

    The best thing can be to use the 'Disable Property' in expressions and giving the expression as per the condition. Just search how to use the disable property.

    0 讨论(0)
  • 2021-01-11 17:59

    How about a simple solution instead of some of the more complex ones that have already been given. For the task you want to conditionally skip, add an expression to the disabled property. Any expression that produces a true or false result will work, so for the question example you could use:

    ISNULL(@[User::MY_VAR]) || @[User::MY_VAR]==""
    

    The only downside is that it may not as visible as some of the other solutions but it is far easier to implement.

    0 讨论(0)
  • 2021-01-11 18:01

    I would create a For Loop Container around the task that needs the condition with the following conditions (@iis the loop counter, @foo is your user variable that you want to test):

    1. InitExpression: @i=0
    2. EvalExpression: @i<1 && !ISNULL(@Foo) && @Foo!=""
    3. AssignExpression: @i=@i+1
    0 讨论(0)
  • 2021-01-11 18:05

    there is no need to create a "script" I think the best (and simpler) approach is to add a blank script task inside your loop container before your "first task", drag the green arrow from it to your "first task" (which obviously will become the second) and use the precedence constraint to do the check.

    To do that, double click the arrow, select "expression" on the "evaluation operation" and write your expression. After hitting OK the arrow will become blue indicating that it isnt a simple precedence constraint, it has a expression assigned to it.

    0 讨论(0)
提交回复
热议问题