Syndicate

Loop through ADO recordset in SSIS Print E-mail
Written by Stanislav Duben   
Thursday, 23 April 2009

2-sql-servers.png

For some solutions in SSIS you may need to go over all records from some SELECT and each record use in some subqueries. In this case you want loop record per record in some task series. Best solution is to use Looping through recordset. Because of SSIS is intuitive enough to create this task, here is step by step overview. In this step by step tutorial, I am using AdventureWorks database from Microsoft. This database was created for testing purpose and is ideal for our example.




Step 1 - Create source recordset

As first we must create new Execute SQL task and open its parameters.

Loop-through-recordset-ScriptTask.jpg 

In general settings ResultSet must be Full result set. To SQLStatement we can put any SQL query we want use as source for Loop through. I am using in example this query:

 

SELECT ProductID, [Name]
FROM Production.Product with (nolock)
WHERE ListPrice > 50 AND ListPrice < 100

 

Loop-through-recordset-Select-full-resultset.jpg

The most important part is in Result Set page. We specify that we want use a variable for this recordset. Right now we need to create some variable. In Result Set tab in Variable Name column you can select <New variable ...> and create it.

Loop-through-recordset-Add-variable.jpg

In this example I am calling it objProduct. This variable must be OBJECT type.

 

When we create it, we have in the Result Name column something like this "NewResultName". This must be overwritten to "0". Because of SSIS is not working properly without numbers.

Loop-through-recordset-Setup-resultset-variable.jpg

 

Step 2 - Create loop

Now we can create Foreach Loop Container by drag and drop and connect our Query Task with this Container.

Loop-through-recordset-LoopContainer.jpg

We have to open properties of this container now and open Container page. Select Foreach ADO Enumerator. Then select objProduct from previous step as an ADO object source variable. Enumeration mode should be set to Rows in the first table.

Loop-through-recordset-Loop-Collection-ADO-variable.jpg

On next page, Variable Mapping, here we can setup variables using inside loop. You select variable (if it doesn't exist yet you can create it from here) and specify which column value will be inserted into by Index number. First column in resultset is 0, second is 1 etc. 

Loop-through-recordset-Loop-variable-mapping.jpg

The loop environment is ready and we can create whatever we want inside loop.

 

Step 3 - Inside loop

 

Right now you are able to create whatever you want based on record by record walkthrough. Just for example I will create simple Script Task now. This task will print product name for each record from resultset.

 

Loop-through-recordset-ScriptTask.jpg

 

I created Script Task and set variables for reading which I want to use.

 

Loop-through-recordset-ScriptTask-variable.jpg
  

 

Inside the Design Script I am using just simple Visual Basic script, that shows a message box for each loop:

 

MsgBox(CType(Dts.Variables("ProductName").Value, String))
Dts.TaskResult = Dts.Results.Success

 

Download example

And that's it. Because best is just take a look at example and play with it, here you can download this example SSIS package and try to work it.

 

Download example SSIS package

 

Tag it:
Delicious
Digg
YahooMyWeb
Technorati
 

Comments  

 
0 #17 adminCR 2012-01-27 21:46
That is so damn perfect. I have learned something new today. Thanks for this blog
Quote
 
 
0 #16 Joe Schmoe 2012-01-05 22:20
Thanks a lot! I spent 2 hours trying to get FOREACH ADO enumerator to work until I found this example.
Quote
 
 
0 #15 Erin 2012-01-05 18:02
Thanks! This helped me get exactl what I needed very quickly.
Quote
 
 
0 #14 geos 2011-12-21 04:21
Thanks
Quote
 
 
0 #13 ndles 2011-09-28 12:22
Thanks for the sample,

I'm just wondering if you know of a way to insert a resultset stored in a variable of type object into a sql table.

I have the part where the object is holding the resultset of a stored procedure call.

Any suggestions welcome
Thanks
Quote
 
 
0 #12 Stanislav Duben 2011-08-18 22:15
Quoting Scotty:
Great article, but a question I have is how do you raise an event/error if no rows were returned by the select into the object?

It looks like I am going to need an expression in either the precedence constraint or the foreach loop to make it fail when the collection is empty, but I can't seem to work out how to access the object returned.


If Loop return error you can use on error arrow (red one) or you can setup condition step before with some Count, and based on results go to loop or not.
Quote
 
 
0 #11 Stanislav Duben 2011-08-18 22:13
Quoting tran:
This is very helpful. Thanks! Question: How do you determine the number of records in the result set?


In first step you can use SELECT Count(*) FROM ... save result as variable. But this loop is FOR EACH, that means you don't need use count as parameter. This Loop do it automatically.
Quote
 
 
0 #10 Scotty 2011-06-13 08:36
Great article, but a question I have is how do you raise an event/error if no rows were returned by the select into the object?

It looks like I am going to need an expression in either the precedence constraint or the foreach loop to make it fail when the collection is empty, but I can't seem to work out how to access the object returned.
Quote
 
 
0 #9 tran 2011-05-19 00:55
This is very helpful. Thanks! Question: How do you determine the number of records in the result set?
Quote
 
 
+1 #8 Arthi 2011-04-08 15:58
Thank you sooo much!...your blog helped me a lot!
Quote
 

Add comment


Security code
Refresh

< Prev