how to use foreach with "for each from variable enumator" if variable is of List<> type in SSIS packages.
Foreach Item enumerator to enumerate items that are collections. For example, you can enumerate the names of executables and working directories that an Execute Process task uses. Foreach Nodelist enumerator to enumerate the result set of an XML Path Language (XPath) expression.
A foreach loop is very similar to a for loop, except there is no evaluation phase. This is because you are looping over a collection of objects, and for each object in the collection, you execute a specific statement.
In the SSIS Toolbox, expand Containers, and then drag a Foreach Loop Container onto the design surface of the Control Flow tab. Right-click the new Foreach Loop Container and select Edit. In the Foreach Loop Editor dialog, on the General page, for Name, enter Foreach File in Folder. Select OK.
You have to declare two SSIS variables
Let's say you have a List<string>
and you need to iterate through its items.
Here is a sample how to do it:
create a sample script task that will fill the "col" collection and add the "User::col" variable to list of the tasks ReadWriteVariables. The script body would be following:
List<string> col = new List<string>() {"One", "Two", "Three"};
Dts.Variables["User::col"].Value = col;
create a Foreach loop container and configure it to type "From variable enumator" over variable "User::Col".
create a sample script task within the Foreach container, demonstrating consuming of the iteration (add the "User::s" to task's ReadOnlyVariables). The script body would be following:
string val = (string)Dts.Variables["User::s"].Value;
MessageBox.Show(val);
Note: the script samples are written in c# for BIDS 2008.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With