SSIS For Loop Container
The For Loop Container is a Control Flow Container that provides similar looping functionality as the T-SQL WHILE statement. The For Loop Container will execute all the Control Flow Tasks placed inside its boundaries until the evaluation condition defined in the EvalExpression section evaluates to False.
Figure 1. For Loop Container
The For Loop Container runtime properties are defined in the For Loop Editor. The For Loop Editor is accessed by right-clicking the For Loop Container and selecting Edit or simply by double-clicking it.
Figure 2. For Loop Editor
In the For Loop Container you specify an initialization expression (InitExpression), an evaluation expression (EvalExpression) and an assignment expression (AssignExpression). The expressions defined in the For Loop Editor are written using SQL Server Integrations Services Expression Language.
Table 1. For Loop Properties
Property |
Required |
Description |
Example |
InitExpression | No |
Initializes variable for use as loop counter. | @MyVar = 1 |
EvalExpression | Yes |
Evaluates if loop should continue or stop. The expression must be a Boolean condition. Looping will stop once the expression evaluates to FALSE. |
@MyVar <= 5 |
AssignExpression | No |
Changes loop counter value after each iteration. Typically, the loop counter variable's value is incremented or decremented. |
@MyVar = @MyVar +1 |
Following the examples from Table 1 above, the For Loop Container shown in Figure 3 will iterate through the three tasks exactly five (5) times in the order shown.
Figure 3. For Loop Container with three tasks