Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to use list collection for "foreach" in SSIS

Tags:

ssis

how to use foreach with "for each from variable enumator" if variable is of List<> type in SSIS packages.

like image 764
Rahul Somwanshi Avatar asked Sep 09 '10 06:09

Rahul Somwanshi


People also ask

What is for each item enumerator in SSIS?

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.

What is the difference between for loop and foreach loop container in SSIS?

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.

How do I create a foreach loop container in SSIS?

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.


1 Answers

You have to declare two SSIS variables

  • the collection variable (source for the For each enumerator)
  • the variable for one item (used within the enumerator)

Let's say you have a List<string> and you need to iterate through its items. Here is a sample how to do it:

  1. in SSIS variables window create variable named "col", type "object"
  2. create variable named "s", type "string"
  3. 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;
    
  4. create a Foreach loop container and configure it to type "From variable enumator" over variable "User::Col".

  5. in the Foreach container variable mappings add a mapping for the "User::s" variable
  6. 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);
    
  7. execute the sample by pressing F5 in BIDS. It should display three dialog boxes with texts "One", "Two", "Three".

Note: the script samples are written in c# for BIDS 2008.

like image 184
Jan Šotola Avatar answered Sep 24 '22 08:09

Jan Šotola