Sie sind auf Seite 1von 6

SELECT SQL and upgrade your Performance - Loop through ADO recordset in SSIS Page 1 of 6

July
search...
26
2010
Username

Lost Password?

No account yet?
Register

Home MSSQL MySQL Utilities CSV to SQL

Latest News Popular

• Solved - Retrieving the COM class • How to make LIMIT (from, to) in MSSQL
factory for component with CLSID in 2005
SSIS • Object search in MSSQL 2005
• Find all tables with column name • Convert online CSV to SQL query
• How to merge 2 tables into one result • OUTPUT command in MSSQL 2005
in MySQL • OUTPUT command and varibles in
• How to drop column with default MSSQL 2005
constraint
• Loop through ADO recordset in SSIS

Main Menu

Home
MSSQL
www.Aptech-Education.com Ads by Google
MySQL
Utilities
Home
CSV to SQL

Loop through ADO recordset in SSIS


Written by Stanislav Duben €€
Internet Explorer cannot
Thursday, display
23 April 2009 the webpage
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
What you 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
€ Diagnose Connection Problems
AdventureWorks database from Microsoft. This database was created for testing purpose and is
€ ideal for our example.
More

Step 1 - Create source recordset


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

• Atlanta Travel Guide


• Berlin Travel Guide
• Brazil Travel Guide
• Bulgaria Travel Guide
• Caribbean Travel Guide
• Detroit Travel Guide
• Egypt Travel Guide
• Hong Kong Travel
Guide
• Japan Travel Guide
• Madrid Travel Guide
• Miami Travel Guide
• New York Travel Guide
• Norway Travel Guide
• Prague Travel Guide €
• Rome Travel Guide In general settings ResultSet must be Full result set. To SQLStatement we can put any SQL query we want use as source
• Shanghai Travel Guide for Loop through. I am using in example this query:
• Slovakia Travel Guide €

http://www.select-sql.com/mssql/loop-through-ado-recordset-in-ssis.html 7/26/2010
SELECT SQL and upgrade your Performance - Loop through ADO recordset in SSIS Page 2 of 6

• Spain Travel Guide


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

€
Syndicate

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.

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.

http://www.select-sql.com/mssql/loop-through-ado-recordset-in-ssis.html 7/26/2010
SELECT SQL and upgrade your Performance - Loop through ADO recordset in SSIS Page 3 of 6

Step 2 - Create loop


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

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.

http://www.select-sql.com/mssql/loop-through-ado-recordset-in-ssis.html 7/26/2010
SELECT SQL and upgrade your Performance - Loop through ADO recordset in SSIS Page 4 of 6

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.€

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.
€

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

http://www.select-sql.com/mssql/loop-through-ado-recordset-in-ssis.html 7/26/2010
SELECT SQL and upgrade your Performance - Loop through ADO recordset in SSIS Page 5 of 6

€€
€
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:
€
Comments
€
#4 Vicky 2010-06-24 20:48 0
Thanks so much!!! So detailed and to the point. This was BIG help!!!!!
Quote
€

€
#3 Jane 2010-01-26 09:36 0
THank you. The example you provided has been most helpful.
Quote
€

€
#2 Chris Page 2009-12-21 16:38 0
I've just spent an hour trying to make this work! Your code works perfectly. Interpreted to mine it fails. Was
reaching that "throw the machine out the window" stage when I had an idea. When embedded in other code this
seems to only work when embedded in a sequence container to ensure that the EST happens before the FeLC.
Quote
€

€
#1 harty 2009-08-21 12:16 0
Just wanted to say Thank you for your blog. I really appreciate such resources where people write about real life,
real problems, real joy... Your resource belongs to my favorites.
Quote
€

Refresh comments list


RSS feed for comments to this post.

Add comment
Name (required)

E-mail (required)

Website

http://www.select-sql.com/mssql/loop-through-ado-recordset-in-ssis.html 7/26/2010
SELECT SQL and upgrade your Performance - Loop through ADO recordset in SSIS Page 6 of 6

1000 symbols left

c Notify me of follow-up comments


d
e
f
g

Refresh

Send

JComments

< Prev
[ Back ]

Created by: Stanislav Duben, all rights reserved. Used template: sun:blogger
Hosted by: AfroDita hry online | Zásady ochrany osobních údajů

http://www.select-sql.com/mssql/loop-through-ado-recordset-in-ssis.html 7/26/2010

Das könnte Ihnen auch gefallen