Being used to DTS 2000, I was very thrilled to see Foreach Loop Container in action with SSIS 2005. It took me some time to figure out how to make this correctly work for my scenario, but once done it worked like a charm!
Mission: To be able to import all .csv files from a particular location to a SQL table, move the file to an archive folder. All of these files had same file structure, but different file names.
Plan:
I am assuming that you are familiar with creating and opening an SSIS Package.
1. Drag and drop the Foreach Loop Container (Found under Control Flow Items in Toolbox) in ControlFlow area.
2. Add 5 new variables in the Variables pane (Right click anywhere in Control Flow area and click on Variables)
Name / Data Type
varFileName / String
varSourceFolder / String
varFilePath / String
varArchiveFolder / String
varArchivePath / String
3. Now its time to change the variables' properties in the Properties pane.
Change the EvaluateAsExpression property to True for varFilePath, varArchiveFolder and varArchivePath.
Change the Expression property as follows:
varFilePath: @[User::varSourceFolder] + "\\" + @[User::varFileName]
varArchiveFolder: @[User::varSourceFolder] + "\\Archived"
varArchivePath: @[User::varArchiveFolder] + "\\" + @[User::varFileName]
4. Double click on the Foreach Loop Container to open its properties.
Click on the 'Collection' section on the left and set the following properties:
Folder: This is where your csv files are saved
Files: this is where you enter your naming pattern - *.csv for all your csv files or ABC*.csv for all your csv files beginning with ABC
Retrieve file name: make sure Name and Extension radio button is checked
5. Now, add the Data Flow Task. Set your source connection as follows and complete the transformation:
Now, we need to move the completed (imported) files to our 'archive' folder.
6. Drop the File System Task (available under Control Flow Items in Toolbox pane) inside the Foreach Loop Container
and make sure its properites are set as follows:
Hit F5 and Enjoy!
Recommended Links:
SSIS Expression Functions: http://msdn2.microsoft.com/en-us/library/ms141671.aspx
Jamie Thomson's Blog 'SSIS Junkie' at http://blogs.conchango.com/jamiethomson/default.aspx
TechRepublic: http://blogs.techrepublic.com.com/datacenter/?p=237
Table Based Foreach Loop: http://www.sqljunkies.com/WebLog/knight_reign/archive/2005/03/25/9588.aspxMission: To be able to import all .csv files from a particular location to a SQL table, move the file to an archive folder. All of these files had same file structure, but different file names.
Plan:
I am assuming that you are familiar with creating and opening an SSIS Package.
1. Drag and drop the Foreach Loop Container (Found under Control Flow Items in Toolbox) in ControlFlow area.
2. Add 5 new variables in the Variables pane (Right click anywhere in Control Flow area and click on Variables)
Name / Data Type
varFileName / String
varSourceFolder / String
varFilePath / String
varArchiveFolder / String
varArchivePath / String
3. Now its time to change the variables' properties in the Properties pane.
Change the EvaluateAsExpression property to True for varFilePath, varArchiveFolder and varArchivePath.
Change the Expression property as follows:
varFilePath: @[User::varSourceFolder] + "\\" + @[User::varFileName]
varArchiveFolder: @[User::varSourceFolder] + "\\Archived"
varArchivePath: @[User::varArchiveFolder] + "\\" + @[User::varFileName]
4. Double click on the Foreach Loop Container to open its properties.
Click on the 'Collection' section on the left and set the following properties:
Folder: This is where your csv files are saved
Files: this is where you enter your naming pattern - *.csv for all your csv files or ABC*.csv for all your csv files beginning with ABC
Retrieve file name: make sure Name and Extension radio button is checked
5. Now, add the Data Flow Task. Set your source connection as follows and complete the transformation:
Now, we need to move the completed (imported) files to our 'archive' folder.
6. Drop the File System Task (available under Control Flow Items in Toolbox pane) inside the Foreach Loop Container
and make sure its properites are set as follows:
Hit F5 and Enjoy!
Recommended Links:
SSIS Expression Functions: http://msdn2.microsoft.com/en-us/library/ms141671.aspx
Jamie Thomson's Blog 'SSIS Junkie' at http://blogs.conchango.com/jamiethomson/default.aspx
TechRepublic: http://blogs.techrepublic.com.com/datacenter/?p=237
Regards,
Ankeet Shah
Please consider the environment before printing this blog! Go Green!
Comments
http://www.sqllion.com/2009/06/programming-foreach-loop-container-%e2%80%93-enumerating-excel-sheets/
MicroStrategy Online Training india
MicroStrategy Online Training hyderabad