Sunday 3 November 2013

For Loop container in SSIS with example

Today in this article i will explain about one of the containers For Loop container. For Loop container groups set of tasks together and allows you to looping through the tasks in your package which is similar like for loop in any programming language.

  • You can a send a mail for set of people.
  • You can loop through an SQL statement and perform certain action until end of the table

Drag and drop For Loop container from tool box on to Control flow design editor as shown in Figure 1.

image Figure 1.

Double click on For Loop container task which takes us to configuration editor as shown in Figure 2.

image Figure 2

Configuration editor has only two options For Loop and Expressions.

InitExpression: This will initialize the loop with specified values

EvalExpression: specifies an expression to stop the loop when the expression evaluates to false. This expression must evaluate to a Boolean.

AssignExpression: specifies an expression that changes a condition in the same way each time the loop iterates.Which is optional one.

We will create a simple example which deletes one row from table product at time

Steps to Follow:

Step1: execute below queries which creates product table with id and name columns and insert 6 rows.

create table product(prodid int,name nvarchar(10))

insert into product values (1, 'prod1');
insert into product values (2, 'prod2');
insert into product values (3, 'prod3');
insert into product values (4, 'prod4');
insert into product values (5, 'prod5');
insert into product values (6, 'prod6');

image

Step2: Drag and drop For loop container and configure as shown below

image

Step3: Drag and drop Execute SQL task inside For Loop container as shown below. Here, Execute SQL task will be executed until EvalExpression condition fails. In this example Execute SQL task will be executed 6 times.

image

 Step4: Configure Execute SQL task in such a way it should delete one row based up on prodid which matches with @count variable from product table.

For the purpose this example, Connection property is specified with the AdventureWorks connection where Product table is created and SQLSourceType is specified as Direct input and SQLStatement is delete from product where prodid = ?. ? is a parameter and which will be replaced with @count variable during run time. @count variable will be attached to ? parameter under Parameter Mapping tab.After configuration is done task editor should reflect like below Figure.

image

Step5: Execute the Package which deletes all 6 rows from product table If configuration is properly set. Refer Below figures where are displayed after execution of package.

imageControlFlow Design editor  

image ProgressBar After execution

NOTE: Please leave comment if you like the post

14 comments:

  1. recently i came your blog and have been read along...thank u for sharing nice examples..it's very helpful to me..we are giving msbi online training

    ReplyDelete

  2. 26.There are lots of information about latest technology and how to get trained in them, like this have spread around the web, but this is a unique one according to me. The strategy you have updated here will make me to get trained in future technologies. By the way you are running a great blog. Thanks for sharing this.
    msbi training in Hyderabad

    ReplyDelete
  3. I think SSIS is the best component to perform a broad range of data migration tasks in SSIS.

    SSIS Postgresql Read

    ReplyDelete