SSIS: execute first task if condition met else ski

2020-04-03 07:28发布

问题:

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.
The first task needs only to get executed on condition that a certain user variable is not null or empty.
Otherwise, the flow should skip the first task and continue to the second one.

How would I go about realizing this (in detail) ?

回答1:

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.

:{>



回答2:

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.



回答3:

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.



回答4:

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


回答5:

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.



回答6:

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.

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.

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.



回答7:

That's a bit tricky.

You have to create a Script Task and check if your variable is not null in there.

So first you have the script task in which you will have the following code in your Main() function:

public void Main()
{
    if (Dts.Variables["User::yourVariable"].Value != null)
    {
        Dts.TaskResult = (int)ScriptResults.Failure;
    }
    else
    {
        Dts.TaskResult = (int)ScriptResults.Success;
    }
}

Then you create two connections from your script task, one to the task that needs to be executed when your variable is not null, and one to the next task (or to another script, if you need to check again, if the variable is not null).

Then you right-click on the (green) arrow of your first connection and select "Failure". Right-click the connection to the next task / script and set it to "Completion".

It should then look something like this:

That's it.