Question

Within a Foreach loop in SSIS is there a way that if a task fails you can break out of the loop to the next iteration?

I am looping over xml files and performing a lookup using values within this file, if the lookup doesn't return any values i'd like to report on this and then not perform any other tasks for this file. If there is no equivalent to a break statement how else can this be achieved?

Was it helpful?

Solution

You could also use a 'for' loop with a boolean condition such as looping while a variable is equal to true. Then when you want to break out of that loop, simply change the value of that variable to false and then you will break out of the loop.

Answering your question...a foreach loop, loops over a collection and other enumerable constructs so long as they exist to loop over. So you could either find a workaround, or just use a 'for' loop instead of a 'foreach' loop. That way you have more of a programming type control over the loop because you set the condition expression.

OTHER TIPS

The lookup can redirect if there are no values returned, away from the successful flow.

You need the rest of your foreach loop to know there has been an error, so one way would be to set a package variable on error, just before you do the logging.

Then, in the 'success' arrow after your lookup you can change it to a conditional success, so that it only proceeds if the value of the variable is not the error value.

So I just had that problem and solved it by a) directing the failed task to a dummy task which did nothing and ended and b) setting the 'FORCEEXECUTIONRESULTS' to 'SUCCESS', which plowed through just the way I wanted it to.

And yet another way would be to put a sequence container into your loop then put the conditional steps in the sequence container. Any decision that should 'continue' need only exit the sequence container. Very simple to implement with all the control you could want including error trapping.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top