posted on Friday, March 03, 2006 10:05 AM by bknight

Looping through a ADO Resultset in SSIS

 Sometimes there is a special case where you’d like to use the results of a query in SSIS and loop through it record by record to perform a series of tasks. You should use these types of incidents very carefully since SSIS wasn’t truly meant to do this over thousands of records. This is useful however if you have a client table and you wish to loop through that table and execute an Execute Package task to load that client’s data. There are many other uses I’ve found for this but the client example is probably the central one.

 

Let’s try a quick example using the AdventureWorks database to execute a Script task inside the loop for each variable. To configure this, let’s first go ahead and create a connection to the AdventureWorks database. With the connection created, we’re going to pull data from a small table (HumanResources.Shift) to do a quick demonstration.

 

Next, create two variables with no default value. The objHumanResource variable will have the data type of Object and the strShift variable will be a string. You can create a variable by right-clicking in the design area and selecting Variables. Make sure the variables have the scope of the entire package and not an individual container or task.

 

Drag an Execute SQL task onto the design pane. Name the task Read Shift Table and point it to the AdventureWorks connection. Use the following directly inputted query:

 

SELECT * FROM HumanResources.Shift.

 

 

 

In the General page of the Execute SQL task, select Full result set for the ResultSet option. This option tells the task that you wish to output the results of the query to some variable. Go to the Result Set page and add a new line in the Result Set grid by clicking Add. For the Result Name column, replace NewResultName with 0 and set the Variable Name column to User::objHumanResource. The final screen should look like the below screenshot. This is telling the task that you wish to output the first result set from the query (0) to the variable.

 

 

Drag over a Foreach Loop Container and connect the green arrow from the Execute SQL task onto the container. In the Collection page, select Foreach ADO Enumerator. Select the source variable of User::objHumanResource and the mode should be set to Rows in the first table.

 

 

 

For the Variable column, select User::strShift. For the Index column type 1. This is making the container output the 2nd column from the rowset into the variable as it iterates through the loop. If you wanted the first column, you'd use the Index of 0.

 

 

Now, you can do whatever you want inside the container. For my example, I placed a Script task inside the container and made a little Hello World example by using the following script (and of course passing in the strShift variable in the ReadOnlyVariables option of the Script task):

 

MsgBox(Dts.Variables("strShift").Value)

 

That’s all there is to it. Again, you would never want to use this on thousands of records but dozens of records would scale nicely. The main use I use it for is to loop through a list of clients in a multi-client ASP type environment and perform a series of actions with it.

 

You can download the package here to get you started.

 

-- Brian Knight

Comments

# re: Looping through a ADO Resultset in SSIS

Wednesday, March 22, 2006 11:00 AM by John Hoge
Thank you so much. I desperately needed a sample like this and could only find loop examples using file folders.

I still need to figure out how to use a value from the loop in a sql task within it but at least this got me past the initial block of using a SQL task result set in a for each loop. *Very* much appreciated!

# re: Looping through a ADO Resultset in SSIS

Sunday, June 18, 2006 5:44 AM by ChrisY
Thanks for the example. It's not easy finding one that is simple and easy to understand. I'm still having trouble trying to use the variable inside the loop. All I want to do is use it to execute a stored procedure, but the value of the variable is not being passed in correctly. If anyone has suggestions it would be much appreciated.

# Looping through a ADO Resultset in SSIS How-to Video

Tuesday, September 26, 2006 5:33 AM by Brian Knight, SQL Server MVP
A while ago I write a quick post on how to loop through an ado recordset in ADO. It seems to be a common...

# SSIS: ForEach Looping through a recordset

Friday, April 06, 2007 6:36 PM by Louis Davidson
I was trying to find a way to repeatedly run the same MDX CREATE GLOBAL CUBE statement 70 times, building

# re: Looping through a ADO Resultset in SSIS

Sunday, May 06, 2007 3:48 AM by goran
Many, many thanks. That "0" for the result name had impressed me enormously. Do you know who in Microsoft is responsible for such a great idea? Would never think of something like that. Is it documented anywhere?

# re: Looping through a ADO Resultset in SSIS

Thursday, May 17, 2007 4:18 AM by David Caulfield
Great clear example.

Points to note though for those who can't return anything for strShift. The variable index in the loop container is the number of the column returned from the sql query. More crucial though is that when you move the script task over from the toolbox to the loop container, it must be placed in the loop container box. Not placed outside it and moved into it after like I did. SSIS will give no error if you do this. Hope that helps people.

# re: Looping through a ADO Resultset in SSIS

Friday, May 25, 2007 12:34 PM by joshcsmith13
One BIG thing that was incidentally (or strategically?) avoided in this example is the way that the ExecuteSQL task handles non-string values. In effect, they get converted to strings (http://blogs.msdn.com/mattm/archive/2007/04/18/why-can-t-i-store-my-bigint-result-in-an-int64-variable.aspx). If you are not aware of this fact, you may be frustrated by numerous data type conversion errors when the ForEach Loop attempts to assign the local variable.

# re: Looping through a ADO Resultset in SSIS

Thursday, September 06, 2007 1:44 PM by RMoore
This example was excellent but I need some help taking it a step farther. I am trying to dynamically set my global variables. My table reurns 2 columns GVStrShiftCol1, GVStrShiftCol2. I can use the code you included to provide them back to the screen but how do I actually create them? I tried using DTS.Variables.Add but I didn't get very far. Is the VB script task the best method or is something else beter?

# re: Looping through a ADO Resultset in SSIS

Wednesday, October 03, 2007 8:14 AM by Al Howard
I am new to using SSIS and am being assigned a task of converting a VB6 program into SSIS. I found your video of the foreach loop helpful but I just want to clarify one area and seek any advice that you may have. My execute SQL Task is returning 54 columns. I understand that I place the full result set in an object variable. In the configuration of the foreach loop, I assume that I set up a variable for each column in the result set. I then have a data flow task that each row will will need to be processed through generating files to be ftped. Anyway, when I run the project, it is never going into the data flow task. What am I doing wrong? Do I have the configuration set up wrong? Again, I am new to this so please excuse my ignorance here. Any help and assistance would be appreciated.

# re: Looping through a ADO Resultset in SSIS

Tuesday, December 18, 2007 11:28 AM by bmxjcsffac

# re: Looping through a ADO Resultset in SSIS

Friday, January 11, 2008 7:32 AM by Shyam
Dear Brian, It will be nice if you can answer some of the questions. They read your article with intrest.

Nice example, but incomplete.

It would have been more useful if extended to demonstrate how to use Execute SQL task inside the loop.

# re: Looping through a ADO Resultset in SSIS

Wednesday, March 05, 2008 11:41 PM by lucdqfhktr

# re: Looping through a ADO Resultset in SSIS

Tuesday, March 25, 2008 12:56 AM by kiujlqe

# re: Looping through a ADO Resultset in SSIS

Wednesday, July 30, 2008 3:10 AM by RKF
Most usefull example i have managed to find, actually managed to get my first SSIS working :)

Thank you