The Result Data Newsletter   
Volume 704 - April 2007   
© Copyright 2007 Result Data Consulting, Ltd.  614-505-0770  www.resultdata.com   

    Result Data Home Page  |  Newsletter Archive  |  Upcoming Events  |  Classes & Workshops  |  Request Information
Upcoming Events: 5/2: MOBOUG, 6/6: Microsoft BI Seminar; 7/11: Business Objects Seminar

Return to Newsletter Contents...

 

SSIS Looping with and ADO Recordset (Part 1)

by: Sean Beal, Senior Consultant, CRCP

 

Recently, we had the opportunity to work with a client who needed an import text file created from data contained in a SQL Server database.  The import text file was unique in that a single file needed to contain both parent and child records, sequentially.  Due to the relatively low volume of data and the unique logic needed to facilitate this, it was decided that this might be a good opportunity to use the new Foreach Loop container in SQL Server 2005 Integration Services (SSIS).

Enumerating records or files in SQL Server 2000 Data Transformation Services (DTS) was tricky at best.  The Foreach Loop Container in SSIS makes this much, much easier, and provides functionality for looping through a collection of files, an ADO Recordset, an Array and more.  In this example, we’ll be using an Execute SQL Task to create an ADO ResultSet;  we’ll then use package variables to pass information to the Foreach Loop Container, where we’ll add lines to the text file based on the current record’s value.

This article assumes that you have created a new SSIS package and connected to a data source (for help on this, see the Help menu in the SQL Server Business Intelligence Development Studio). After this, the first step is to create the two variables we will use; we can add variables from the Variables window (View | Other Windows | Variables).  The first variable will be of the data type Object, and will be used to hold our ResultSet.  The second variable will be of the data type String, and will be used to hold the ID of each record as it is enumerated.  We’ll call our object variable ‘company’ and our string variable ‘company_id’ (see Figure 1a).

Figure 1a

The next step is to create our ADO ResultSet using the Execute SQL Task (figure 1b).  Add an Execute SQL Task to the package by dragging it onto the workspace.  We’ll name the task ‘Get Companies’.  Double click the task to edit the task properties, enter the connection created earlier, and enter the SQL statement you wish to use in the SQLStatement property.  For this example, we will use a stored procedure called rsp_company_list.  Finally, change the ‘Result Set’ property from ‘None’ to ‘Full Result Set’.  This will allow the results of our stored procedure to be output to our object variable.  Finally, click on the ‘Result Set’ option in the left hand pane to go to the Result Set page.  Click ‘Add’ to add a new item to the list.  Change the default Result Name to 0, and select the ‘company’ variable created earlier as the Variable Name.  This assigns the first result set (0) from our stored procedure to the object variable ‘company’ (Figure 2a).  Click on ‘OK’ to exit the Task Editor.

Figure 1b

Figure 2a

We can now add our Foreach Loop container to the package by dragging it onto our work surface.  Connect the Execute SQL Task by selecting it and dragging the green arrow to the Foreach Loop Container.  Double click the Foreach Loop Container to edit the properties, and select the ‘Collection’ option in the right hand pane.  We need to change the Enumerator type from the default of ‘Foreach File’ to ‘Foreach ADO Enumerator’.  We will also need to configure the Enumerator to use the company variable as its object source (Figure 2b).

 Figure 2b

Click on the ‘Variable Mappings’ option in the right hand pane to map our string variable (company_id) to the desired column of our result set (in this case 0, since we want the first column of a zero based list).  The result should look like Figure 2c.

Figure 2c

At this point, we’ve defined two variables and used them in conjunction with a SQL Task to feed a Foreach Loop Container.  Next month, we’ll define the processes and tasks within the container that occur for each record passed in.  See you then!

Go to Top  |  Return to Newsletter Contents

 

The Result Data Newsletter is published approximately once a month to share the latest information on business intelligence, data management and CRM. There should be a link below to allow you to change or remove yourself from our list. We take your requests very seriously. If you have any difficulty please contact us at 614-505-0770 and we will make sure that your request is handled properly. This is not intended to be an unsolicited message and you can reach us in person if needed.

© Copyright 2007 Result Data Consulting, Ltd. - All Rights Reserved
All trademarks and copyrights are the property of their respective owners. This information is provided without warranty.
Announcements
Implementing XI R2 Business Views Class in May
If you need to create Business Views in BusinessObjects Enterprise XI, you need to attend this class.  BusinessObjects XI R2: Implementing Business Views will be held May 21/22 and is guaranteed to run.

Click on the class name to get additional  information or to register.
Next Microsoft BI Seminar
The next free Microsoft BI seminar on is June 7th, 2007.  Call 614-505-0770 or click here to reserve your seat.
Private Training Classes & Mentoring
Ask us about our private classes and mentoring services and how they can help your team get up the curve fast. 
Next MOBOUG Meeting
The next Mid-Ohio Business Objects User Group (MOBOUG) meeting is May 2nd, 2007.  Call 614-505-0770 or click here to reserve your seat.
Looking for a Few Good Men and Women
Join our award winning team of Business Intelligence consultants and .Net Software developers.

Send your resume and salary requirements to:
jobs@resultdata.com