Introduction
How can we configure our foreach loop container, in case any of the inner task fails. This scenarios might not be common but at times we may need to configure our foreach loop container to such scenarios.
Background
Suppose you have an application, where we need to loop through some log table based on the IDs & load data into the destination. Now, in this scenario there might be the stiuation where some of the tasks in foreach loop container may fail. But your requirement is even though the inner tasks failwe should process the other source which are available with us.
Using the code
- Add a new package, name it as "ContinueExecutionOfFailedForEachLoop".
- In this example, I am using Foreach loop for File Enumerator.
- Create a 2 variables as "varFullFile", this will store the full file path when you are looping.
e.g. "C:\MSBI_Training_Materails\Source\Test.txt" - Another variable as "varFile", here we will store the source path.
e.g. "C:\MSBI_Training_Materails\Source\
I will tell you more why we created 2 variables. - Add a ForEach loop container & name it as "FELC_Text_Files",Now we will configure the ForEach
Loop container as below screen shots
- Double click on ForEach Loop container, go to the "Collection" tab.
- In the "Enumerator" select "Foreach File Enumerator".
- Now in the "Enumerator Configuration", select the path of your source folder.
- In "Files", specify the file type as "Test*.txt". This step will make sure that from the source folder only the files which start from "Test" & extension ".txt" will be processed.
- Now, Go to the Variable mapping tab & shown the image.
- In this tab, Select the variable which we created as "varFullFile". This will contain the full path as well as filename with extension.
- This will configure your ForEach loop container.
- Now we will add some source file in our source folder with below structure.
ID,Val
1,2
2,3
3,4
4,5
5,6
6,7
You can create some 4 to 5 source file with the same structure & name each file as Test1,Test2 etc.. - Now drag & drop an Data Flow Task, name it as "DFT_TransferTextFiles".
- In the Data Flow, add Flat File Source & name it as "SRC_Text_Files".
- This will create a new Flat File connection in the Connection Manager area, Rename it as "TestConn".
- This is our source connection string. Now go to the properties of the TestConn i.e. the Flat File connection.
- Go to the expressions, from the propery select "ConnectionString" property.
- In the Expressions Builder, drag the variable "varFullFile" & evaluate the expression. This will show the full file path & file name.
- Now drag & drop Flat File Destination & rename it as "DESTN_Text_Files".
- This will also create new Flat File connection in the Connection Manager area, rename it as "Destn Test".
- Now go to the Properties of "Destn Test" connection string, again select the ConnectionString property.
- Give the below expression in the Expression Builder
- @[User::varDestnFileName]+ "Destn"+REPLACE( @[User::varFullFile], @[User::varFile] , "")
- This we are doing to get different files at the destination & we will name it as "DestnTest1" where Test1 is the
Source file names. - The source file which we created earlier now we will introdue some error in the 3rd file as below.
ID,Val
1,2222222222222222222222222222222222222222222222222222222222222222222
2,3333333333333333333333333333333333333333333333333333333333333333333
- This will make sure that our package fails for Truncation error.
- Now execute the package, as you execute you will see for the 3rd file the package fails.
- Now to execute the foreach loop container even after the failure we need to do the below steps.
- Select the Data Flow Task, go to the Event Handlers.
- Enable the OnError Event handler.
- In the Event Handler tab, click on the "Show System Variables".
- Now select the "Propogate" property & change its value to "False".
- This will ensure that the parent control i.e. ForEach loop will not know about the error in the child task.
- Once you set this, try executing the package. Now the same package which failed previously will run without any error.
- Now its the developers duty to keep track of the errors which happend.
- Please note suppose you have some 4 to 5 task in ForEach loop container, then in such scenarios we can add an Sequence Container. All the child task will be placed in this Sequence container.
- Then we will enable the Event handler on the sequence container & make its Propogate property to False. This is the short cut to achieve this task or else we have to individually configure each & every child tasks Propogate property..
Points of Interest
When we set the Propagate property to False, the parent control will not come to know about the failure in the child task
Links
http://simonworth.wordpress.com/2009/11/11/ssis-event-handler-variables-propagate/.