Shredding a Recordset

Doing what to a recordset?

Shredding a recordset in this instance means that we are going to show you how to take a recordset produced in your SSIS package, loop over the rows in that recordset, break apart the columns and do something with them. This is really useful when you want to preform an action on a row of data for every row of data just like we are going to do here. Sure we could use an ExecuteSQL task to get the recordset as well but that does limit our choices of source data whereas doing it in the pipeline does not. Something useful we hope.

Retrieving the Recordset

There are a couple of things that we are going to have to setup first though

The Variables

This task requires four variables. It needs one to hold the recordset and 3 to hold the column values when we shred the recordset. Let's configure those now. In control flow we have a menu titled SSIS and if we drop that down we see Variables as one of the menu choices. Here is how the variables look after completion


The Connections

For this package we are going to need two connections. One for the source data and one for the SMTP server with which we are going to send our mails.

To set up an OLE DB Connection Manager we will right click in the Connection Manager tray at the bottom of our package and choose New OLE DB Connection


We click New and configure as seen in the graphic.


Click OK after doing that and let's create the SMTP connection manager. Same drill as before, right click in the Connection Manager tray only this time we choose New Connection. That brings us to the following screen where we now choose the SMTP Connection as shown.


We then have to configure the connection manager and luckily it is one of, if not the, most easy to set up of all.


If you look in the Connection Manager tray now you should see something very similar to the following


The DataFlow Task

This task in this example is about one thing and that is getting the data we want into a variable so we can use it later in the package. We are going to need to fill our recordset with something so we will need to define a statement that retrieves our rows. Imagine we have sales people who take contact details from potential customers. They must contact those people within 30 days and update our CRM system. If they do not then we want to let them know through eMail that they are overdue.

So now we have something to put into a recordset let's build the means by which we can get the data. Begin by adding a Data Flow task to the Control Flow. Double click on the task to enter the Data Flow section of our package. Now drag from the toolbox onto the design surface the following:

  • An OLE DB Source
  • A Recordset Destination

The first thing we are going to do is configure the OLE DB Source so we double click on it to open up its editor


In here we are going to specify the connection manager built earlier, an Access Mode of SQL Command and a SQL command text. The graphic above shows this completed

If we look to the left of this editor we can see a columns branch on the tree underneath the connection manager branch. This is where we need to tell the source what columns from our statement we want to flow down the pipeline so we click on that now and request that all columns be sent.


Now we need to configure the recordset destination. Before we do that we drag the green connection coming from the OLE DB source and simply attach it to the recordset destination. This will give the destination the ability to know what to expect in terms of structure. After attaching the connector we double click on the recordset destination.


The thing to see on this page is the VariableName property to which we give the name of the variable in which we want to store the recordset (rsDetails).

Just like we did on the source we need to configure some columns and we do that by clicking on the Input Columns tab. We want to write all columns to the destination so we select all the available columns.


When we now come out of the destination editor our Data Flow looks like this.


Breaking It Apart

We have seperated this part of setup because it is logically distinct from the previous phase. This is where we are going to grab the recordset, break it apart and use the values we get from breaking it apart and do something with them.

The ForEach Container

The ForEach Loop container allows us to loop over a collection of objects. We can, using this container, loop over a number of inbuilt collections but in this article we are in interested in only one of the collections, the ADO Enumerator. When the enumerator loops over the collection it returns at most one instance of the collection on each iteration. That object needs to be passed out to the tasks inside the container and we do that through a variable. In this article we use the variable User::rsDetails. We drop a ForEach Loop container onto designer from the toolbox. After double clicking on it we choose from the tree on the left Collection. The following graphic shows our collection chosen and variable assigned.


So what happens then? Well on each iteration of the loop a row from the recordset is assigned to the variable with in our case three columns. We now need to Shred it. We now click on the Variable Mappings item in the tree. The following graphic shows our variables configured to accept the columns in the recordset.


What''s happening here is that the variables on the left that we configured earlier are being assigned to the column indexes, on the right, in the recordset and they are zero based. We have no expressions to configure so that''s it for this task. Right so we have our columns in the row mapped to variables and we need to do something with them. That comes now with the Send Mail Task

The Send Mail Task With Expressions

This does as the name suggests and for us this is the way that we are going to communicate to our employees that they have not followed up on their leads. Remember the SMTP Connection Manager we configured earlier well this where we are going to use it. Drop a Send Mail task onto the designer and place it inside the ForEach Loop container.


We double click on the task and after configuration it looks now like this.


We haven't configured all the properties we need because we are going to use property expressions to do that in a moment so we click on Expressions in the tree on the left.


As you can from the graphic above there are ellipses to the right and we need to click on them now. The window we now see is where we will choose a property on the left hand side of the window and then assign an expression on the right. Again we see here ellipses and this will take us to a great expression editor for the property. The window is shown below.


The thing about property expressions is that they are evaluated at runtime. We can build quite complex expressions to map to our properties. In this article we need three properties to be dynamic and part of the expressions will be the variables we grabbed earlier. The properties we need to configure are:

  • Message Source
  • ToLine
  • Subject

The following three graphics show these properties in the expression editor with their expressions set.


The Message Source

One thing to note about this window is that we have pressed the Evaluate Expression button and as you can see our expression is evaluated and displayed. This gives you an idea of what it ill look like when this happens at runtime



The ToLine



The Subject



Now with all our expressions set the expressions window looks like this


That''s it for expressions so the last thing we need to do is join the green connector from the Data Flow Task onto the Foreach Loop Container and we''re done.



This article has demonstrated a number of new things in SSIS and we hope you have found it useful. Be sure to let us know if you think the Recordset Enumerator is useful and any interesting uses to which you may put it.