Receintly I was working on a project where I needed to use a foreach loop with an SSIS project, but couldn’t for the life of me get it to properly process the XML document which I was giving it. Well with some major work and digging I was able to get it working correctly, but it took me for ever to get all the little setting correct so I figured that I’d throw the info up here for anyone else who is looking for it.
Some background on what the process is that I’m working on. Basically I’ve got a table with catagorized data in it. I need to export all the data from the table info one file per catagory (don’t ask, I didn’t design it, I’ve just got to automate it; and it actually makes sence in the grant schem of things). Well I figured that the easiest way to do this was to use a foreach loop and give it an XML document with the list of catagories to process. (This was better than looping through and getting the next value from the database.)
So needless to say, I get started on my little process.
The query which I’m using within an Execute SQL Task is below. The Execute SQL Task puts the XML data into an SSIS variable called v_CategoryList with a data type of string.
SELECT WebsiteBlockCategoryId as id
FROM dbo.WebsiteBlockCategory cat with (nolock)
where WebsiteBlockCategoryId <> 0
for XML AUTO
The XML document looks like this. (SSIS seams to be wrapping it within <ROOT></ROOT> tags for me which is why I’m not doing it my self.) My XML document is actually must longer than this, but you get the idea. It’s a very basic XML document.
<cat id=”18″ />
<cat id=”19″ />
<cat id=”20″ />
<cat id=”21″ />
<cat id=”22″ />
<cat id=”23″ />
<cat id=”24″ />
<cat id=”25″ />
<cat id=”26″ />
<cat id=”27″ />
<cat id=”28″ />
As you can see on the screenshot of the forloop properties I’ve set the source to be my variable and the EnumerationType to Element Collection. Since I know where the data is within the XML document I use DirectInput for both the outer and Inner XPath strings. For the Outer XPath string I’m using “//cat”. Because I’m not putting in the ROOT level name it doesn’t matter what gets put in there as long as there is a parent level. For the Inner XPath string I’ve got the Element name with an @ sign in front of it “@id”.
Over on the Variables page of the UI I’ve got my v_CategoryId variable mapped to Index 0 of my document. If you need to output more than one element from your XML document set your Inner XPath to “*”. This “should” allow you to bring back all the elements and refer to them by index number starting with 0. I havn’t actually tried this, as I’ve always only needed a single element hense the “should”.
And to think, that little thing took me a couple of days to get working right. I can only imagine trying to do this in SQL 2000 and DTS.
DennyContact the Author | Contact DCAC