While building an ETL package, sometimes we come across to repeat same types of tasks many times i.e. like to do certain task depending upon the number of students in the class, or any other condition, etc. So instead of developing same containers or tasks so many times, it’s always better to iterate it with certain parameters.
To resolve the above situation, SQL Server Integration Service (SSIS) comes with one special container called “For Loop Container“. This container has the same logic that a “For loop” keyword have in leading programming languages. It will iterate through all the tasks inside it till the condition satisfies. It acts as a repeating control flow. For each iteration of the loop, the For Loop container evaluates an expression that is specified and repeats its workflow until the expression evaluates to False.
Following are the elements used to configure For Loop Container:
- InitExpression: (Optional) Specifies an expression that initializes variable values used in the loop. This is optional because even if the value is not initialized, it will take the default value assigned to the corresponding variable as its initial value.
- EvalExpression: This is the terminating condition for the Loop. Specifies an expression that stops the loop when the expression evaluates to False. The resulting value of this expression must be a Boolean value.
- AssignExpression: (Optional) Specifies an expression that changes a condition in the same way each time the loop iterates. This works like incrementing condition in ‘For’ operators in programming languages, i.e. i++, j–.
- Expression Pane: Here we have a collection of Expressions. The evaluation result of each expression is assigned to a property and replaces the value of the property. It is basically used to dynamically assign the Expression parameters for the above elements.
The below diagram shows an example for the implementation of FOR LOOP Container in a SSIS package. It has a package variable name @counter which is set to 0 initially. Our objective is to run a task (to insert a record in a given table) five times so that the table will be populated five times with the same record.

So to get this done, the elements of the FOR LOOP Container has to be set as follows:
- InitExpression: @Counter = 0 [Optional as the value is already initialized to 0]
- EvalExpression: @Counter < 5
- AssignExpression @Counter = @Counter + 1

A For Loop container can have only one evaluation expression. But it can be nested with other FOR LOOPs as many times, thus can be used to implement complex looping requirements in the package.
The Table structure for the [dbo].[Employee_Dim] table is:
|
Column_name
|
Type
|
|
employee_Sl_No
|
int
|
|
emp_name
|
varchar
|
|
emp_id
|
int
|
|
emp_DOB
|
datetime
|
The “Create Employee Table Task” will create the table named [dbo].[Employee_Dim] in the database, if the table does not exists.
Statement to create table:
/****** Object: Table [dbo].[Employee_Dim] ******/
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N‘[dbo].[Employee_Dim]‘) AND type in (N‘U’))
CREATE TABLE [dbo].[Employee_Dim](
[employee_Sl_No] [int] IDENTITY(1,1) NOT NULL,
[emp_name] [varchar](50),
[emp_id] [int] NULL,
[emp_DOB] [datetime] NULL CONSTRAINT [DF_Employee_Dim_emp_DOB] DEFAULT (getdate())
) ON [PRIMARY]
Then the For Loop will iterate for 5 times executing Insert Record (Employee Dim). This Execute SQL Task will insert one record to the table [dbo].[Employee_Dim] each time it is executed.
Statement to INSERT record:
INSERT INTO [dbo].[Employee_Dim]
([emp_name]
,[emp_id]
,[emp_DOB])
VALUES
(‘EmpName’ + Cast(? as varchar)
,101
,getdate())
Here, Employee Name is derived from the variable @Counter. So, Employee Name will be different for each iteration.

After successful execution of the package, the result set would be like this:

Let’s do the same by Programming Integration Services. Here, the package will be created first and then an Execute SQL Task named “Create Employee Table Task” will be added to it along with the corresponding Connection Manager named “LocalHost.TesTDB“. After that, the For Loop Container will be added containing the task “Insert Record (Employee Dim)”.
The code below shows the above package programmatically and the code itself is self-descriptive. The code for SQL server 2005 and SQL Server 2008 are a bit different; both the approaches are given below.
Add the below References before developing the code.
Microsoft.SqlServer.Dts.Design
Microsoft.SqlServer.DTSPipelineWrap
Microsoft.SQLServer.DTSRuntimeWrap
Microsoft.SQLServer.ManagedDTS
Programming Integration Service in SQL Server 2005
C# .Net Expand
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SqlServer.Dts.Runtime;
using Microsoft.SqlServer.Dts.Pipeline.Wrapper;
using System.Data.SqlClient;
using System.Xml;
//The below code will create a SSIS package with For Loop Container for SQL Server 2005
namespace CreatePackageSample
{
class Program
{
static void Main(string[] args)
{
//To Create a package named [Sample Package]
Package package = new Package();
package.Name = "Sample Package";
package.PackageType = DTSPackageType.DTSDesigner90;
package.VersionBuild = 1;
//For destination database (TesTDB)
ConnectionManager OLAP = package.Connections.Add("OLEDB");
OLAP.ConnectionString = "Data Source=.;Initial Catalog=TesTDB;Provider=SQLNCLI.1;Integrated Security=SSPI;Auto Translate=False;";
OLAP.Name = "LocalHost.TesTDB";
//To add package variables
Variable Counter = package.Variables.Add("Counter", false, "User", 0);
Counter.Name = @"Counter";
//To add Create Employee Table Task (Execute Sql task )
TaskHost CreateEmployeeTableTaskTask = (TaskHost)package.Executables.Add("Microsoft.SqlServer.Dts.Tasks.ExecuteSQLTask.ExecuteSQLTask, Microsoft.SqlServer.SQLTask, Version=9.0.242.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91");
CreateEmployeeTableTaskTask.Name = @"Create Employee Table Task";
CreateEmployeeTableTaskTask.Description = @"Execute SQL Task Description";
XmlDocument CreateEmployeeTableTaskTaskdoc = new XmlDocument();
CreateEmployeeTableTaskTaskdoc.LoadXml(@"<SQLTask:SqlTaskData SQLTask:Connection=""LocalHost.TesTDB"" SQLTask:SqlStmtSourceType=""DirectInput"" SQLTask:SqlStatementSource=""/****** Object: Table [dbo].[Employee_Dim] ******/
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Employee_Dim]') AND type in (N'U'))
CREATE TABLE [dbo].[Employee_Dim](
[employee_Sl_No] [int] IDENTITY(1,1) NOT NULL,
[emp_name] [varchar](50),
[emp_id] [int] NULL,
[emp_DOB] [datetime] NULL CONSTRAINT [DF_Employee_Dim_emp_DOB] DEFAULT (getdate())
) ON [PRIMARY]"" SQLTask:ResultType=""ResultSetType_None"" xmlns:SQLTask=""www.microsoft.com/sqlserver/dts/tasks/sqltask""></SQLTask:SqlTaskData>");
((IDTSComponentPersist)CreateEmployeeTableTaskTask.InnerObject).LoadFromXML(CreateEmployeeTableTaskTaskdoc.DocumentElement, null);
//For Loop Container
ForLoop ForLoopContainer = (ForLoop)package.Executables.Add("STOCK:FORLOOP");
ForLoopContainer.FailPackageOnFailure = true;
ForLoopContainer.FailParentOnFailure = true;
ForLoopContainer.Name = @"For Loop Container";
ForLoopContainer.Description = @"For Loop Container";
ForLoopContainer.InitExpression = "@Counter = 0";
ForLoopContainer.EvalExpression = "@Counter < 5";
ForLoopContainer.AssignExpression = "@Counter = @Counter + 1";
//To add Insert Record (Employee Dim) (Execute Sql task )
TaskHost InsertRecordTask = (TaskHost)ForLoopContainer.Executables.Add("Microsoft.SqlServer.Dts.Tasks.ExecuteSQLTask.ExecuteSQLTask, Microsoft.SqlServer.SQLTask, Version=9.0.242.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91");
InsertRecordTask.Name = @"Insert Record (Employee Dim)";
InsertRecordTask.Description = @"Execute SQL Task Description";
XmlDocument InsertRecordTaskdoc = new XmlDocument();
InsertRecordTaskdoc.LoadXml(@"<SQLTask:SqlTaskData SQLTask:Connection=""LocalHost.TesTDB"" SQLTask:SqlStmtSourceType=""DirectInput"" SQLTask:SqlStatementSource=""INSERT INTO [dbo].[Employee_Dim]
([emp_name]
,[emp_id]
,[emp_DOB])
VALUES
('EmpName' + Cast(? as varchar)
,101
,getdate())"" SQLTask:ResultType=""ResultSetType_None"" xmlns:SQLTask=""www.microsoft.com/sqlserver/dts/tasks/sqltask""><SQLTask:ParameterBinding SQLTask:ParameterName=""0"" SQLTask:DtsVariableName=""User::Counter"" SQLTask:ParameterDirection=""Input"" SQLTask:DataType=""3"" /></SQLTask:SqlTaskData>");
((IDTSComponentPersist)InsertRecordTask.InnerObject).LoadFromXML(InsertRecordTaskdoc.DocumentElement, null);
//To set the precedence for the above tasks and containers
//Precedence from "Create Employee Table Task" to "For Loop Container"
PrecedenceConstraint CrtEmpTabTsk_ForLoopCont = package.PrecedenceConstraints.Add(package.Executables[@"Create Employee Table Task"], package.Executables[@"For Loop Container"]);
CrtEmpTabTsk_ForLoopCont.Name = @"CrtEmpTabTsk_ForLoopCont";
//Saving the package
Microsoft.SqlServer.Dts.Runtime.Application app = new Microsoft.SqlServer.Dts.Runtime.Application();
app.SaveToXml(@"I:\Site work\SamplePackage\SamplePackage\SamplePackage.dtsx", package, null);
}
}
}
VB .Net Expand
Imports System
Imports System.Collections.Generic
Imports System.Text
Imports Microsoft.SqlServer.Dts.Runtime
Imports Microsoft.SqlServer.Dts.Pipeline.Wrapper
Imports System.Data.SqlClient
Imports System.Xml
'The below code will create a SSIS package with For Loop Container for SQL Server 2005
Module Module1
Sub Main(ByVal args As String())
'To Create a package named [Sample Package]
Dim package As New Package()
package.Name = "Sample Package"
package.PackageType = DTSPackageType.DTSDesigner90
package.VersionBuild = 1
'For destination database (TesTDB)
Dim OLAP As ConnectionManager = package.Connections.Add("OLEDB")
OLAP.ConnectionString = "Data Source=.;Initial Catalog=TesTDB;Provider=SQLNCLI.1;Integrated Security=SSPI;Auto Translate=False;"
OLAP.Name = "LocalHost.TesTDB"
'To add package variables
Dim Counter As Variable = package.Variables.Add("Counter", False, "User", 0)
Counter.Name = "Counter"
'To add Create Employee Table Task (Execute Sql task )
Dim CreateEmployeeTableTaskTask As TaskHost = DirectCast(package.Executables.Add("Microsoft.SqlServer.Dts.Tasks.ExecuteSQLTask.ExecuteSQLTask, Microsoft.SqlServer.SQLTask, Version=9.0.242.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91"), TaskHost)
CreateEmployeeTableTaskTask.Name = "Create Employee Table Task"
CreateEmployeeTableTaskTask.Description = "Execute SQL Task Description"
Dim CreateEmployeeTableTaskTaskdoc As New XmlDocument()
CreateEmployeeTableTaskTaskdoc.LoadXml("<SQLTask:SqlTaskData SQLTask:Connection=""LocalHost.TesTDB"" SQLTask:SqlStmtSourceType=""DirectInput"" SQLTask:SqlStatementSource=""/****** Object: Table [dbo].[Employee_Dim] ******/" & vbCr & vbLf & "IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Employee_Dim]') AND type in (N'U'))" & vbCr & vbLf & "CREATE TABLE [dbo].[Employee_Dim](" & vbCr & vbLf & vbTab & "[employee_Sl_No] [int] IDENTITY(1,1) NOT NULL," & vbCr & vbLf & vbTab & "[emp_name] [varchar](50)," & vbCr & vbLf & vbTab & "[emp_id] [int] NULL," & vbCr & vbLf & vbTab & "[emp_DOB] [datetime] NULL CONSTRAINT [DF_Employee_Dim_emp_DOB] DEFAULT (getdate())" & vbCr & vbLf & ") ON [PRIMARY]"" SQLTask:ResultType=""ResultSetType_None"" xmlns:SQLTask=""www.microsoft.com/sqlserver/dts/tasks/sqltask""></SQLTask:SqlTaskData>")
DirectCast(CreateEmployeeTableTaskTask.InnerObject, IDTSComponentPersist).LoadFromXML(CreateEmployeeTableTaskTaskdoc.DocumentElement, Nothing)
'For Loop Container
Dim ForLoopContainer As ForLoop = DirectCast(package.Executables.Add("STOCK:FORLOOP"), ForLoop)
ForLoopContainer.FailPackageOnFailure = True
ForLoopContainer.FailParentOnFailure = True
ForLoopContainer.Name = "For Loop Container"
ForLoopContainer.Description = "For Loop Container"
ForLoopContainer.InitExpression = "@Counter = 0"
ForLoopContainer.EvalExpression = "@Counter < 5"
ForLoopContainer.AssignExpression = "@Counter = @Counter + 1"
'To add Insert Record (Employee Dim) (Execute Sql task )
Dim InsertRecordTask As TaskHost = DirectCast(ForLoopContainer.Executables.Add("Microsoft.SqlServer.Dts.Tasks.ExecuteSQLTask.ExecuteSQLTask, Microsoft.SqlServer.SQLTask, Version=9.0.242.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91"), TaskHost)
InsertRecordTask.Name = "Insert Record (Employee Dim)"
InsertRecordTask.Description = "Execute SQL Task Description"
Dim InsertRecordTaskdoc As New XmlDocument()
InsertRecordTaskdoc.LoadXml("<SQLTask:SqlTaskData SQLTask:Connection=""LocalHost.TesTDB"" SQLTask:SqlStmtSourceType=""DirectInput"" SQLTask:SqlStatementSource=""INSERT INTO [dbo].[Employee_Dim]" & vbCr & vbLf & " ([emp_name]" & vbCr & vbLf & " ,[emp_id]" & vbCr & vbLf & " ,[emp_DOB])" & vbCr & vbLf & " VALUES" & vbCr & vbLf & " ('EmpName' + Cast(? as varchar)" & vbCr & vbLf & " ,101" & vbCr & vbLf & " ,getdate())"" SQLTask:ResultType=""ResultSetType_None"" xmlns:SQLTask=""www.microsoft.com/sqlserver/dts/tasks/sqltask""><SQLTask:ParameterBinding SQLTask:ParameterName=""0"" SQLTask:DtsVariableName=""User::Counter"" SQLTask:ParameterDirection=""Input"" SQLTask:DataType=""3"" /></SQLTask:SqlTaskData>")
DirectCast(InsertRecordTask.InnerObject, IDTSComponentPersist).LoadFromXML(InsertRecordTaskdoc.DocumentElement, Nothing)
'To set the precedence for the above tasks and containers
'Precedence from "Create Employee Table Task" to "For Loop Container"
Dim CrtEmpTabTsk_ForLoopCont As PrecedenceConstraint = package.PrecedenceConstraints.Add(package.Executables("Create Employee Table Task"), package.Executables("For Loop Container"))
CrtEmpTabTsk_ForLoopCont.Name = "CrtEmpTabTsk_ForLoopCont"
'Saving the package
Dim app As New Microsoft.SqlServer.Dts.Runtime.Application()
app.SaveToXml("I:\Site work\SamplePackage\SamplePackage\SamplePackage.dtsx", package, Nothing)
End Sub
End Module
Programming Integration Service in SQL Server 2008
C# .Net Expand
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SqlServer.Dts.Runtime;
using Microsoft.SqlServer.Dts.Pipeline.Wrapper;
using System.Data.SqlClient;
using System.Xml;
//The below code will create a SSIS package with For Loop Container for SQL Server 2008
namespace SamplePackageCSharp2008
{
class Program
{
static void Main(string[] args)
{
//To Create a package named [Sample Package]
Package package = new Package();
package.Name = "Sample Package";
package.PackageType = DTSPackageType.DTSDesigner100;
package.VersionBuild = 1;
//For destination database (TesTDB)
ConnectionManager OLAP = package.Connections.Add("OLEDB");
OLAP.ConnectionString = "Data Source=.;Initial Catalog=TesTDB;Provider=SQLNCLI.2;Integrated Security=SSPI;Auto Translate=False;";
OLAP.Name = "LocalHost.TesTDB";
//To add package variables
Variable Counter = package.Variables.Add("Counter", false, "User", 0);
Counter.Name = @"Counter";
//To add Create Employee Table Task (Execute Sql task )
TaskHost CreateEmployeeTableTaskTask = (TaskHost)package.Executables.Add("Microsoft.SqlServer.Dts.Tasks.ExecuteSQLTask.ExecuteSQLTask, Microsoft.SqlServer.SQLTask, Version=10.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91");
CreateEmployeeTableTaskTask.Name = @"Create Employee Table Task";
CreateEmployeeTableTaskTask.Description = @"Execute SQL Task Description";
XmlDocument CreateEmployeeTableTaskTaskdoc = new XmlDocument();
CreateEmployeeTableTaskTaskdoc.LoadXml(@"<SQLTask:SqlTaskData SQLTask:Connection=""LocalHost.TesTDB"" SQLTask:SqlStmtSourceType=""DirectInput"" SQLTask:SqlStatementSource=""/****** Object: Table [dbo].[Employee_Dim] ******/
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Employee_Dim]') AND type in (N'U'))
CREATE TABLE [dbo].[Employee_Dim](
[employee_Sl_No] [int] IDENTITY(1,1) NOT NULL,
[emp_name] [varchar](50),
[emp_id] [int] NULL,
[emp_DOB] [datetime] NULL CONSTRAINT [DF_Employee_Dim_emp_DOB] DEFAULT (getdate())
) ON [PRIMARY]"" SQLTask:ResultType=""ResultSetType_None"" xmlns:SQLTask=""www.microsoft.com/sqlserver/dts/tasks/sqltask""></SQLTask:SqlTaskData>");
((IDTSComponentPersist)CreateEmployeeTableTaskTask.InnerObject).LoadFromXML(CreateEmployeeTableTaskTaskdoc.DocumentElement, null);
//For Loop Container
ForLoop ForLoopContainer = (ForLoop)package.Executables.Add("STOCK:FORLOOP");
ForLoopContainer.FailPackageOnFailure = true;
ForLoopContainer.FailParentOnFailure = true;
ForLoopContainer.Name = @"For Loop Container";
ForLoopContainer.Description = @"For Loop Container";
ForLoopContainer.InitExpression = "@Counter = 0";
ForLoopContainer.EvalExpression = "@Counter < 5";
ForLoopContainer.AssignExpression = "@Counter = @Counter + 1";
//To add Insert Record (Employee Dim) (Execute Sql task )
TaskHost InsertRecordTask = (TaskHost)ForLoopContainer.Executables.Add("Microsoft.SqlServer.Dts.Tasks.ExecuteSQLTask.ExecuteSQLTask, Microsoft.SqlServer.SQLTask, Version=10.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91");
InsertRecordTask.Name = @"Insert Record (Employee Dim)";
InsertRecordTask.Description = @"Execute SQL Task Description";
XmlDocument InsertRecordTaskdoc = new XmlDocument();
InsertRecordTaskdoc.LoadXml(@"<SQLTask:SqlTaskData SQLTask:Connection=""LocalHost.TesTDB"" SQLTask:SqlStmtSourceType=""DirectInput"" SQLTask:SqlStatementSource=""INSERT INTO [dbo].[Employee_Dim]
([emp_name]
,[emp_id]
,[emp_DOB])
VALUES
('EmpName' + Cast(? as varchar)
,101
,getdate())"" SQLTask:ResultType=""ResultSetType_None"" xmlns:SQLTask=""www.microsoft.com/sqlserver/dts/tasks/sqltask""><SQLTask:ParameterBinding SQLTask:ParameterName=""0"" SQLTask:DtsVariableName=""User::Counter"" SQLTask:ParameterDirection=""Input"" SQLTask:DataType=""3"" /></SQLTask:SqlTaskData>");
((IDTSComponentPersist)InsertRecordTask.InnerObject).LoadFromXML(InsertRecordTaskdoc.DocumentElement, null);
//To set the precedence for the above tasks and containers
//Precedence from "Create Employee Table Task" to "For Loop Container"
PrecedenceConstraint CrtEmpTabTsk_ForLoopCont = package.PrecedenceConstraints.Add(package.Executables[@"Create Employee Table Task"], package.Executables[@"For Loop Container"]);
CrtEmpTabTsk_ForLoopCont.Name = @"CrtEmpTabTsk_ForLoopCont";
//Saving the package
Microsoft.SqlServer.Dts.Runtime.Application app = new Microsoft.SqlServer.Dts.Runtime.Application();
app.SaveToXml(@"I:\Site work\SamplePackage 2008\SamplePackage\SamplePackage.dtsx", package, null);
}
}
}
VB .Net Expand
Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Text
Imports Microsoft.SqlServer.Dts.Runtime
Imports Microsoft.SqlServer.Dts.Pipeline.Wrapper
Imports System.Data.SqlClient
Imports System.Xml
'The below code will create a SSIS package with Sequence Container for SQL Server 2008
Module Module1
Sub Main()
'To Create a package named [Sample Package]
Dim package As New Package()
package.Name = "Sample Package"
package.PackageType = DTSPackageType.DTSDesigner100
package.VersionBuild = 1
'For destination database (TesTDB)
Dim OLAP As ConnectionManager = package.Connections.Add("OLEDB")
OLAP.ConnectionString = "Data Source=.;Initial Catalog=TesTDB;Provider=SQLNCLI.2;Integrated Security=SSPI;Auto Translate=False;"
OLAP.Name = "LocalHost.TesTDB"
'To add package variables
Dim Counter As Variable = package.Variables.Add("Counter", False, "User", 0)
Counter.Name = "Counter"
'To add Create Employee Table Task (Execute Sql task )
Dim CreateEmployeeTableTaskTask As TaskHost = DirectCast(package.Executables.Add("Microsoft.SqlServer.Dts.Tasks.ExecuteSQLTask.ExecuteSQLTask, Microsoft.SqlServer.SQLTask, Version=10.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91"), TaskHost)
CreateEmployeeTableTaskTask.Name = "Create Employee Table Task"
CreateEmployeeTableTaskTask.Description = "Execute SQL Task Description"
Dim CreateEmployeeTableTaskTaskdoc As New XmlDocument()
CreateEmployeeTableTaskTaskdoc.LoadXml("<SQLTask:SqlTaskData SQLTask:Connection=""LocalHost.TesTDB"" SQLTask:SqlStmtSourceType=""DirectInput"" SQLTask:SqlStatementSource=""/****** Object: Table [dbo].[Employee_Dim] ******/" & vbCr & vbLf & "IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Employee_Dim]') AND type in (N'U'))" & vbCr & vbLf & "CREATE TABLE [dbo].[Employee_Dim](" & vbCr & vbLf & vbTab & "[employee_Sl_No] [int] IDENTITY(1,1) NOT NULL," & vbCr & vbLf & vbTab & "[emp_name] [varchar](50)," & vbCr & vbLf & vbTab & "[emp_id] [int] NULL," & vbCr & vbLf & vbTab & "[emp_DOB] [datetime] NULL CONSTRAINT [DF_Employee_Dim_emp_DOB] DEFAULT (getdate())" & vbCr & vbLf & ") ON [PRIMARY]"" SQLTask:ResultType=""ResultSetType_None"" xmlns:SQLTask=""www.microsoft.com/sqlserver/dts/tasks/sqltask""></SQLTask:SqlTaskData>")
DirectCast(CreateEmployeeTableTaskTask.InnerObject, IDTSComponentPersist).LoadFromXML(CreateEmployeeTableTaskTaskdoc.DocumentElement, Nothing)
'For Loop Container
Dim ForLoopContainer As ForLoop = DirectCast(package.Executables.Add("STOCK:FORLOOP"), ForLoop)
ForLoopContainer.FailPackageOnFailure = True
ForLoopContainer.FailParentOnFailure = True
ForLoopContainer.Name = "For Loop Container"
ForLoopContainer.Description = "For Loop Container"
ForLoopContainer.InitExpression = "@Counter = 0"
ForLoopContainer.EvalExpression = "@Counter < 5"
ForLoopContainer.AssignExpression = "@Counter = @Counter + 1"
'To add Insert Record (Employee Dim) (Execute Sql task )
Dim InsertRecordTask As TaskHost = DirectCast(ForLoopContainer.Executables.Add("Microsoft.SqlServer.Dts.Tasks.ExecuteSQLTask.ExecuteSQLTask, Microsoft.SqlServer.SQLTask, Version=10.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91"), TaskHost)
InsertRecordTask.Name = "Insert Record (Employee Dim)"
InsertRecordTask.Description = "Execute SQL Task Description"
Dim InsertRecordTaskdoc As New XmlDocument()
InsertRecordTaskdoc.LoadXml("<SQLTask:SqlTaskData SQLTask:Connection=""LocalHost.TesTDB"" SQLTask:SqlStmtSourceType=""DirectInput"" SQLTask:SqlStatementSource=""INSERT INTO [dbo].[Employee_Dim]" & vbCr & vbLf & " ([emp_name]" & vbCr & vbLf & " ,[emp_id]" & vbCr & vbLf & " ,[emp_DOB])" & vbCr & vbLf & " VALUES" & vbCr & vbLf & " ('EmpName' + Cast(? as varchar)" & vbCr & vbLf & " ,101" & vbCr & vbLf & " ,getdate())"" SQLTask:ResultType=""ResultSetType_None"" xmlns:SQLTask=""www.microsoft.com/sqlserver/dts/tasks/sqltask""><SQLTask:ParameterBinding SQLTask:ParameterName=""0"" SQLTask:DtsVariableName=""User::Counter"" SQLTask:ParameterDirection=""Input"" SQLTask:DataType=""3"" /></SQLTask:SqlTaskData>")
DirectCast(InsertRecordTask.InnerObject, IDTSComponentPersist).LoadFromXML(InsertRecordTaskdoc.DocumentElement, Nothing)
'To set the precedence for the above tasks and containers
'Precedence from "Create Employee Table Task" to "For Loop Container"
Dim CrtEmpTabTsk_ForLoopCont As PrecedenceConstraint = package.PrecedenceConstraints.Add(package.Executables("Create Employee Table Task"), package.Executables("For Loop Container"))
CrtEmpTabTsk_ForLoopCont.Name = "CrtEmpTabTsk_ForLoopCont"
'Saving the package
Dim app As New Microsoft.SqlServer.Dts.Runtime.Application()
app.SaveToXml("I:\Site work\SamplePackage 2008\SamplePackage\SamplePackage.dtsx", package, Nothing)
End Sub
End Module
To make your work more easy and effortless,
SQL Lion team comes up with a unique tool named “
SSIS Component Explorer” that will help you in retrieving the
Creation Name, Component Class ID, etc about any component like Control Flow Tasks, Data Flow Transformations, Connections Managers, Log Providers, etc in SSIS package. It also provides code snippets for each component.
Download the free version from
here.
Cast(? as varchar)
the above said statement is not clear, can u able to expain it clearly,
which will be helpfull for me
jayamanjunath ks
+919035342730
Well,
As in the above statement, I am trying to insert employee’s names for each iteration of the loop, so just to distinguish each employee name, I am concatenating constant value “EmpName” with the counter which is of integer data type and is coming by the parameter “User::Counter”, as it is impossible to concatenate one string value to an integer value, hence I am type casting integer to string before concatenation. Here, “?” is the placeholder for the integer value.
Thanks,