Software Development
Blogs and Discussion
developer.*
Books Articles Blogs Subscribe d.* Gear About Home

SSIS Foreach ADO Enumerator: Mapping Columns to Variables by Index

This post was originally published as a complaint that the Foreach ADO Enumerator on the Foreach Container did not map bit columns to Boolean variables properly. I was getting this error:

The type of the value being assigned to variable "User::XXXXXXX" differs from the current variable type. Variables may not change type during execution. Variable types are strict, except for variables of type Object.

Turns out the problem was not data type mismatching (though there are documented problems with BigInt mapping as a string) but rather that the indexes I was using were off. Silly me, I assumed that the first column in my SELECT list would be index 0. Nope. It's index 1. (Not sure what 0 is being used for yet.) I discovered this by reading the results in a script task, using DataReader.GetOrdinal to see where my columns were:

Dim msg As String
msg += "Column1: " + reader.GetOrdinal("Column1").ToString() + vbNewLine
msg += "Column2: " + reader.GetOrdinal("Column2").ToString() + vbNewLine
System.Windows.Forms.MessageBox.Show(msg)

Once I knew the right indexes, I was able to update the Foreach task variable mappings. (It sure would be nice if we didn't have to mess with ordinal indexes anyway--what's wrong with column names?)

(I'll soon post a new blog entry that shows how to get to the DataSet result from the Execute SQL task in a downstream script task.)

Check your index mapping. Update: Doh! Turns out I had a column in my SELECT list at index 0 after all. Lesson learned: triple check the index mapping. This index ordinal stuff is a bit of a pain since the SQL task is disconnected from the Foreach that's using the results. If the query is in an stored proc, you're even more disconnected. If someone else changes the order of the select list in the stored proc (or, , if you're using SELECT *, then you're screwed.

Update: Something else interesting I learned: if your underlying column is nullable, you may need to map to an Object variable to allow for the null. I had a nullable date column mapped to a DateTime SSIS variable and received the above error. I changed the variable to Object and it works. I used a script tasl to check the type of the variable when the value is null and it is System.DBNull. So it looks like you can do something like this to check what you have in the variable:

If TypeOf Dts.Variables("MyNullableDate") Is System.DBNull Then
'Value in underlying column is null.
Else
'Treat as DateTime
End If

Dan

Thank you!

I was getting slammed by nullability until I came across your post. Thank you for posting this! Rick

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Recent comments

User login

About our advertising.

Atom Feed

developer.* Blogs also has an Atom feed, located at this url.

Click here for more information about Atom.

A Jolt Award Finalist
Software Creativity 2.0
Foreword by Tom DeMarco

Recent Posters

Based on most recent 60 days, sorted by # of posts and name.

Google
Web developer.*

Who's online

There are currently 1 user and 19 guests online.

Online users

Syndicate

Syndicate content
All views expressed by authors, bloggers, and commentors are their own and do not necessarily reflect the views of developer.* or its proprietors.
Click to read the Copyright Notice.

All content copyright ©2000-2005 by the individual specified authors (and where not specified, copyright by Read Media, LLC). Reprint or redistribute only with written permission from the author and/or developer.*.

www.developerdotstar.com