Click here to Skip to main content
Click here to Skip to main content

Using the Foreach ADO Enumerator in SSIS

By , 4 Jun 2006
 

Introduction

The goal of this example is to collect some basic data like server name, service name, language, etc. about a list of SQL servers. The server list is stored in a SQL server table ("Server"); the collected data should be stored in table "ServerSetup":

Tables

We start by creating a SSIS package in Visual Studio 2006. The list of servers that should be examined is read from our SQL Server database by using an Execute SQL Task. We need to write the resultset of a SQL query into a variable. The loop will later iterate through all rows of all tables in this variable. It is important to specify Full result set; if you do not set this option, you will not be able to store the resultset into a variable.

Execute SQL Task

After you have entered the SQL statement and you have set the ResultSet option, you have to switch to the Result Set pane and specify the name of the variable in which you want to store the result set. The variable has to be of type Object:

Execute SQL Task

The next step is to create our Foreach ADO Loop. As you can see in the picture below, you have to specify the variable into which we wrote the result set of our Execute SQL Task

Loop Configuration

In addition to that, we have to specify a second variable (of type String!) into which the loop will write the content of each row of the result set. The variable's content will change from iteration to iteration:

Loop Configuration

This picture shows a summary about the variables you have to define for running this sample:

Variables Overview

Inside the loop, we use a data flow task to get the basic data from our various SQL Servers. By now, our control flow should look like this:

SSIS Control Flow

The next step is to create our source connection. Firstly we create a static connection that points to any of the SQL Servers which we want to collect data from:

Source Connection

Secondly, we use the connection's properties to specify an expression through which the connection string of the connection will be changed to the connection string in the variable that is changed by the Foreach ADO loop:

Connection Expression

Last but not least, we define the destination connection and map the fields:

Destination Connection

At the end, the data flow of our package should look like this:

SSIS Data Flow

Try it! You will see that the Foreach ADO Loop changes the content of the variable ConnectionString automatically and you receive the basis data about all your SQL Servers in the target table.

History

  • 4th June, 2006: Initial post

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

r.stropek
software architects
Austria Austria
Member
Hi, my name is Rainer Stropek. I am living a small city named Traun in Austria. Since 1993 I have worked as a developer and IT consultant focusing on building database oriented solutions. After being a freelancer for more than six years I founded a small IT consulting company together with some partners in 1999. In 2007 my friend Karin and I decided that we wanted to build a business based on COTS (component off-the-shelf) software. As a result we founded "software architects" and developed the time tracking software "time cockpit" (http://www.timecockpit.com). If you want to know more about our companies check out my blogs at http://www.software-architects.com and http://www.timecockpit.com or take a look at my profile in XING (http://www.openbc.com/hp/Rainer_Stropek2/).
 
I graduated the Higher Technical School for MIS at Leonding (A) in 1993. After that I started to study MIS at the Johannes Kepler University Linz (A). Unfortunately I had to stop my study because at that time it was incompatible with my work. In 2005 I finally finished my BSc (Hons) in Computing at the University of Derby (UK). Currently I focus on IT consulting, development, training and giving speeches in the area of .NET and WPF, SQL Server and Data Warehousing.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionGood tutorial, but can't use it. Can you give step by step instructions ?memberetl_monkey11 May '13 - 6:44 
I had a problem in setting the value of "ConnectionString" (variables screenshot). I also need steps to configure the "dynamic" connection, ie whose connection string changes.
QuestionError on setting up Source connectionmemberJohn Waclawski29 Nov '12 - 8:19 
I know this is a very old, old article but hopefully someone still maintains it.
I'm using SQL Server 2008 so hopefully not much has changed since this was implemented.
 
I'm on the section about editing the source connection. I'm following it almost verbatim. Within the Data Flow task I drop in my OLE DB Source Object. My ConnectionString looks much different then the one on the screenshot for starters:
 
Data Source=DevSQL01;Provider=SQLNCLI10.1;Integrated Security=SSPI;Application Name=SSIS-Package-{blah-blah-blah-blah}DevSQL01;Auto Translate=False;
 
Then, when I add the expression @[User::ConnectionString] and click OK, my OLE DB object errors out (red x) and I'm immediately dead in the water at that point. I have to delete the OLE DB object & the connection in Connection Manager. Plus my ConnectionString property blanks out.
 
This pretty much is what I am looking for to do what I need to do. Thumbs Up | :thumbsup: Hope someone can help me.
Questionwww.aboutsql.inmemberMember 375892218 Sep '12 - 4:48 
Nice Post
 
Check below links for some more information on Loops in SSIS
 
http://www.aboutsql.in/2012/09/how-to-use-foreach-loop-in-ssis.html[^]
 
http://letslearnssis.blogspot.in/2011/07/for-each-loop.html[^]
QuestionWhat connection string is needed for SSIS to SQL Server 2000?memberGiuseppeMe12 Mar '10 - 5:14 
What connection string is needed for SSIS to SQL Server 2000?
 
I created a source OLDDB to SQL Server 2000, then have a foreach loop changing connection.
 
I was using:
Provider=sqloledb;Data Source=SQLServerName;Integrated Security=SSPI;Initial Catalog=master
 
Then tried:
Data Source=SQLServerName;Initial Catalog=master;Provider=SQLNCLI10.1;Integrated Security=SSPI;Auto Translate=False;
GeneralIt was my connection stringsmemberlianaent26 Jun '09 - 10:50 
they were written for .Net, not for OLD DB. I rewrote them and the package runs successfully, however, I guess I'll always have that red X over my OLE DB Source!
GeneralThis package won't even compilememberlianaent26 Jun '09 - 9:57 
Maybe I'm thick (I've only been in SSIS for a week), but I can't get this thing to work at all. The moment I set the ConnectionString expression (@[User::ConnectionString]) in the properties window of the connection manager, the OLE DB Source becomes invalid. Once opened I can't even close it again without getting an error. Needless to say the package doesn't even start to run, because of the error in the OLE DB Source. I guess you'd call it a compile time error.
 
I tried setting Delay Validation to True - to no avail.
 
I'm forced to hard code the server name, so when the package does run it always calls the same server.
 
Any clues to how to do this are welcome!
 
Larry
GeneralForeach File Enumerator using Excel filesmemberblackpower2k718 Jun '09 - 8:49 
For more information on For each loop enumerators (Foreach File Enumerator using Excel files) and programming integration service (SSIS) using C# .Net and VB .Net, please visit the below link:
http://www.sqllion.com/2009/06/programming-foreach-loop-container-–-enumerating-excel-files/
Big Grin | :-D
 
You will know me better sooner...

Generalfaster extraction and loading on SSIS.memberblackpower2k725 Apr '09 - 19:54 
hi...
check out the below link for getting some tips over faster extraction and loading on SSIS.
 
http://www.sqllion.com/
 
You will know me better sooner...

GeneralRe: Using the Foreach ADO enumerator in SSISmemberMember 422791120 Mar '09 - 3:25 
Hi,
 
I am facing one the above issue like....
 
I have a Config DB from which i will get the source and destination connection strings.So i created 2 variables @Source and @destination with data type string and value is empty.
 
I created a sql task under the result set i mapped with the values from the sql like
 
SQL:
Select srcconnstr,desconnstr from configdb.config
 
strconnstr -- @Source
desconnstr -- @destination
 
After this task i have a lot multiple Dataflow tasks for all those tasks ,under the source and Destination connection managers i mapped to the variable....
 
but it is throwing the error like
 
Error at Prod_Dly_Load_UQA_BU_BUSEGM [Connection manager "UQA_DEST_CONN"]: SSIS Error Code DTS_E_OLEDBERROR. An OLE DB error has occurred. Error code: 0x80004005.
An OLE DB record is available. Source: "Microsoft OLE DB Provider for ODBC Drivers" Hresult: 0x80004005 Description: "[Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified".
 
Error at Prod_Dly_Load_UQA_BU_BUSEGM [Log provider "Prod_Full_Load_UQA_BU_SEGMT"]: SSIS Error Code DTS_E_CANNOTACQUIRECONNECTIONFROMCONNECTIONMANAGER. The AcquireConnection method call to the connection manager "UQA_DEST_CONN" failed with error code 0xC0202009. There may be error messages posted before this with more information on why the AcquireConnection method call failed.
COM error object information is available. Source: "Prod_Dly_Load_UQA_BU_BUSEGM" error code: 0xC0202009 Description: "SSIS Error Code DTS_E_OLEDBERROR. An OLE DB error has occurred. Error code: 0x80004005.
An OLE DB record is available. Source: "Microsoft OLE DB Provider for ODBC Drivers" Hresult: 0x80004005 Description: "[Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified".
".
 
Error at LOAD UQA_BU [Slowly Changing Dimension [158]]: SSIS Error Code DTS_E_CANNOTACQUIRECONNECTIONFROMCONNECTIONMANAGER. The AcquireConnection method call to the connection manager "UQA_DEST_CONN" failed with error code 0xC0202009. There may be error messages posted before this with more information on why the AcquireConnection method call failed.
 
Error at LOAD UQA_BU [DTS.Pipeline]: component "Slowly Changing Dimension" (158) failed validation and returned error code 0xC020801C.
 
Error at LOAD UQA_BU [DTS.Pipeline]: One or more component failed validation.
 
Error at LOAD UQA_BU: There were errors during task validation.
 
Error at Prod_Dly_Load_UQA_BU_BUSEGM [Connection manager "UQA_DEST_CONN"]: SSIS Error Code DTS_E_OLEDBERROR. An OLE DB error has occurred. Error code: 0x80004005.
An OLE DB record is available. Source: "Microsoft OLE DB Provider for ODBC Drivers" Hresult: 0x80004005 Description: "[Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified".
 
(Microsoft.DataTransformationServices.VsIntegration)
 

Thanks in advance....
 
Pradeep
QuestionWhy is my version of package failing with "User::ConnectionString" cannot be appliedmemberAmber Robertson22 Sep '08 - 2:16 
Hi, I have tried this SSIS project but I think I have got the User:ConnectionString variable value incorrect. I typed in the following:
Provider=SQLOLEDB;Data Source=RemoteServerName;User ID=Login; Password=Password
 
I am getting the following errors in Execution Results:
1) Error: ForEach Variable Mapping number 1 to variable "User::ConnectionString" cannot be applied.
2) Error: The type of the value being assigned to variable "User::ConnectionString" differs from the current variable type. Variables may not change type during execution. Variable types are strict, except for variables of type Object.
 
Would you mind telling me where this variable is picking up the connection string from? I suspect I am using the incorrect connection string for my environment.
 
Any suggestions much appreciated.
 
Kind Regards
Amber
AnswerRe: Why is my version of package failing with "User::ConnectionString" cannot be appliedmemberAmber Robertson23 Sep '08 - 5:25 
It was failing because the ConnectionString field was left blank in the Server SQL table and the ConnectionString SSIS variable was getting this value in the Foreach ADO enumerator from the Server table.
GeneralError to the ole db source connection after adding the @connectionString to the 'Source' expressionmemberMember 6813509 Apr '08 - 5:05 
After adding the @connectionString to the 'Source' sql connection, I'm getting an error in the source oledb in the data flow task. What am I doing wrong? Thank you Smile | :)
GeneralProblem with Foreach ADO enumerator in SSIS [modified]memberDavid Branscome12 Feb '08 - 13:13 
Got It
Variable mapping passing the object from the exec sql task to the foreach ADO loop
Picking up the object and assigning it to the string
Using the proper connection string. Recommended Connection string
 
Provider=sqloledb;
Data Source=RemoteServerName;
Initial Catalog=DatabaseName;
User Id=Login;
Password=Password;
 
Smile | :) Cheers and Thank you very much!
 
modified on Thursday, April 3, 2008 5:35 PM

GeneralGreat articlememberTiNgZ aBrAhAm30 Nov '06 - 10:18 
Was breaking my head trying to migrate existing DTS packages using dynamic server name changes to SSIS. Finally I found a way thanks to your article.
 
«If a man does his best, what else is there?»

GeneralInformativememberT-C3 Aug '06 - 11:54 
Thanks for this article! It helped me figure out how to use a Data flow task inside a ForEach loop container. Smile | :)
 
Born to code.

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web02 | 2.6.130516.1 | Last Updated 4 Jun 2006
Article Copyright 2006 by r.stropek
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid