In my previous article I wrote about the For loop Container. In this article I will show how we can implement the Foreach loop container with a demo. The Foreach loop container is the third container amongst the three containers in SSIS.
The Foreach loop container can be used to loop through a set of enumerated objects like files in a folder or rows in a table. We can add different control flow tasks within the container for each enumerator or a DataFlow task to process data related to each item. Let us now implement this container with a small demo.
Implementing Foreach Loop Container
Objective: To copy files from one folder to another folder.
Step 1: We will be using the package we created in my article. Drag and drop a Foreach loop container in the design UI surface -> Drag and drop a File system task inside the container.
Step 2 : Create a variable as shown below with the depicted data type and value
Step 3: create 2 folders named ‘Source’ and ‘destination’ respectively. In the source folder create two files named file1.txt and file2.txt
Step 4: Right click -> Choose Edit on the Foreach loop container and fill up the following as depicted below. Click on OK.
Step 5: Right Click on the File System task -> On DestinationConnection -> Choose the usage type as ‘Existing Folder’ -> Choose the path of the destination folder -> click on ok.
Step 6: Set the rest of the fields as below -> Click on OK.
Now we are ready to test out solution. Press F5 and the 2 files should be in the destination folder.
Bingo!!!! The files has been copied to the destination folder. I hope this article was useful in understanding the Foreach loop and its understanding.