Live Demos

Friday, February 12, 2010

SQL to XML with SSIS




Have you ever had a vendor ask for XML instead of a quick CSV file? Well, if you’re using MSSQL 2005 or 2008, I’m sure you realized just how awesome they are when this request came through. I’m going to walk through exporting an XML file from SQL Server 2005 using SSIS ans SQL.

First we’ll need to write the SQL script to generate the XML. I’m going to write an XML file that exports a list of users to a vendor so we’ll have root » customer » action » user info

Let’s start basic and just get something like this:

<root>
<
customer>demousername</customer>
</
root>

To get this with SQL, it looks like this:

select 'demousername'
for xml path('customer'), root('root')

Really simple so far right? Okay, so I need to add two attributes to my customer element called username and password, which are provided by my vendor. So I’ll want my end XML to look like this:

<root>
<
customer id="demouserid" password="demopassword" />
</
root>

To get this result, I simply write this SQL:

select 'demouserid' as '@id', 'demopassword' as '@password'
for xml path('customer'), root('root')

Okay, so now I need to add my action element like this:

<root>
<
customer id="demouserid" password="demopassword">
<
action ActionType="User_Add"/>
</
customer>
</
root>

The SQL looks like this:

select 'demouserid' as '@id', 'demopassword' as '@password',
(
select 'User_Add' as "@ActionType"
for xml path('action'), type)
for xml path('customer'), root('root')

The only weird thing about this SQL is the “@ActionType”, which represents the attribute name on the action element. The @ sign basically says hey I’m an attribute and not an element. Also, the “type” after (‘action’), says that I’m an element. Without the “type”, you’ll get this:

<root>
<
customer id="demouserid" password="demopassword">&lt;action ActionType="User_Add"/&gt;</customer>
</
root>

Okay, so now I need to add my user info and I’ll end up with this:

<root>
<
customer id="demouserid" password="demopassword">
<
action ActionType="User_Add">
<
FirstName>Deran</FirstName>
<
LastName>Schilling</LastName>
<
UserName>test@demo.com</UserName>
<
Password>demopassword</Password>
<
Email>test@demo.com</Email>
<
JobTitle>Something Awesome</JobTitle>
</
action>
</
customer>
</
root>

So to get this XML, I just add in the columns and names I want like this:

select 'demouserid' as '@id', 'demopassword' as '@password',
(
select 'User_Add' as "@ActionType"
, firstname as FirstName, lastname as LastName
, emailaddress as UserName, 'demopassword' as [Password]
, emailaddress as Email, jobtitle as JobTitle
from demousertable
for xml path('action'), type)
for xml path('customer'), root('root')

One more thing I want to show you is how to add a reference number to the action element. So I’ll end up with something like this:

<root>
<
customer id="demouserid" password="demopassword">
<
action ActionType="User_Add" ref="1">
<
FirstName>Deran</FirstName>
<
LastName>Schilling</LastName>
<
UserName>test@demo.com</UserName>
<
Password>demopassword</Password>
<
Email>test@demo.com</Email>
<
JobTitle>Something Awesome</JobTitle>
</
action>
<
action ActionType="User_Add" ref="2">
<
FirstName>John</FirstName>
<
LastName>Demoed</LastName>
<
UserName>john@demo.com</UserName>
<
Password>demopassword</Password>
<
Email>john@demo.com</Email>
<
JobTitle>Something Okay</JobTitle>
</
action>
</
customer>
</
root>

Notice the ref=”1” and ref=”2” on the action element? The way you get that result is with this SQL:

select 'demouserid' as '@id', 'demopassword' as '@password',
(
select 'User_Add' as "@ActionType", ROW_NUMBER() OVER (ORDER BY username) as '@ref'
, firstname as FirstName, lastname as LastName
, emailaddress as UserName, 'demopassword' as [Password]
, emailaddress as Email, jobtitle as JobTitle
from demousertable
for xml path('action'), type)
for xml path('customer'), root('root')

So the ROW_NUMBER() OVER (ORDER BY uniqueidentifierofsomesort) gets the row number for each returned record. I love that feature.

Alrighty! So now let’s get to the SSIS, which is much simpler!

Step 1:
Create new SSIS package in Visual Studio 2005.

Step 2:
Drag an “Execute SQL Task” over to the “Control Flow” and name it something like “Generate XML” or whatever you prefer.

Step 3:
Double-click it.

  • General Section
    • Change ResultSet to XML
    • Select/Create your Connection
    • Enter the SQL statement, like the one we created above

image

  • Result Set Section
    • Add a new string variable called something like XmlContent

imageimage

Step 4:
Drag a “Script Task” over to the “Control Flow” and name it something like “Save Generated XML” or whatever you prefer.

Step 5:
Double-click it.

  • Script Section
    • Add XmlContent (or whatever you named your variable) in the ReadOnlyVariables
    • Click “Design Script…”

image

Step 6:
Once in the ScriptMain for VB, which I’m not a fan, but appears to be the only option. So you’ll just add this snippet in the Public Sub Main():

Dim sw As New IO.StreamWriter("C:\demo\AddUsers.xml")
sw.Write(Dts.Variables("User::XmlContent").Value.ToString())
sw.Dispose()
Dts.TaskResult = Dts.Results.Success
The red text above represents what you’ll need to change to match what you desire. First item is the file path to the location of your xml file and the second item is the name of your variable with the namespace.

That’s it! Now you have a generated XML file from a SQL DB. I thought it was pretty cool and a LOT easier than I thought it would be when I first started.

Thanks for reading!


Shout it

15 comments:

  1. Greatе post. Keep postіng suсh kіnd of information
    on your site. Im really impressed by your site.

    Ηello thеre, You hаѵe done an incredіble job.
    I'll definitely digg it and in my view recommend to my friends. I am confident they will be benefited from this site.

    http://www.ruralinfopages.com/used-forestry-bucket-trucks/
    My web site ... bucket truck sales

    ReplyDelete
  2. Hmm is anyone else encountering рroblemѕ wіth the pіctures
    on this blοg lοading? I'm trying to determine if its a problem on my end or if it'ѕ the blog.
    Any feedback would bе greatly aρрreciated.


    http://wwω.serіes7eхаmрrep.
    com/
    Here is my web blog :: Stock Trading

    ReplyDelete
  3. What's Going down i am new to this, I stumbled upon this I have found It positively helpful and it has helped me out loads. I am hoping to contribute & assist different users like its aided me. Good job.

    My webpage ... Website
    Feel free to visit my blog ... big green egg

    ReplyDelete
  4. Whаt's Going down i am new to this, I stumbled upon this I have found It positively helpful and it has helped me out loads. I am hoping to contribute & assist different users like its aided me. Good job.

    Feel free to surf to my web site - Website
    my webpage :: big green egg

    ReplyDelete
  5. You actually make it seem so easy with your presentation but
    I find this topic to be really something that I think I would never understand.
    It seems too complicated and very broad for me. I am looking forward for your next post,
    I'll try to get the hang of it!
    Also see my page: Read �

    ReplyDelete
  6. I was recommended this web site through my cousin.

    I am no longer positive whether or not this publish is written by means of him as no one else recognise such special about my trouble.
    You're incredible! Thanks!
    Feel free to surf my weblog :: Oprimal Stack

    ReplyDelete
  7. I dο not even know the waу I stopped up herе, however I belіeѵeԁ thiѕ submit used tο be goοd.
    I don't understand who you're but certainly
    уou're going to a well-known blogger when you are not already. Cheers!

    Also visit my web blog ... bucket trucks

    ReplyDelete
  8. Hi are uѕing Wοrdpreѕs for your site platform?

    I'm new to the blog world but I'm tryіng to get startеd and set up my οwn.

    Do yоu requiгe any html codіng exρertise to make уour own blog?
    Any help would bе greatly appгeciаted!


    my homepage - tens machine

    ReplyDelete
  9. I thinκ this іs one of the most vital informаtion for mе.
    And i am glad rеading your article. But want tο гemark
    on ѕome general thingѕ, The site style is idеal, the аrticles is rеally niсe :
    D. Gοod ϳοb, cheers

    Also visit my web blοg: roofers oklahoma city

    ReplyDelete
  10. Hi there, just wanted to tell you, I enjoyed
    this article. It was funny. Keep on posting!

    Also visit my site: Cash Survey

    ReplyDelete
  11. Ѕωeet blog! I fοund it while seагching on Yahoo News.
    Dο you have any tіpѕ οn hoω tο get listed in
    Yahoo Νews? I've been trying for a while but I never seem to get there! Appreciate it

    Take a look at my page ... how to make money buying and selling cars for profit

    ReplyDelete
  12. Afteг I originallу commentеd I seem to hаvе clicked on the -Nοtify me whеn new comments aге
    added- checkbox and from nοw on evегy time a comment is aԁded I get four
    emails with thе exaсt same сomment.
    Perhaps theгe іѕ a means you can rеmove me from that serѵіce?
    Thanκs a lot!

    Fееl free to visit my web page: how to buy and sell cars for profit

    ReplyDelete
  13. Pretty section of content. I just stumbled upon your
    website and in accession capital to assert that I
    get actually enjoyed account your blog posts. Anyway I will be subscribing to your augment and even I
    achievement you access consistently rapidly.

    Look at my homepage ... http://trimextrindiet.com

    ReplyDelete
  14. I have read so many articles or reviews about the blogger lovers except
    this post is truly a pleasant piece of writing, keep it up.


    My web site - More

    ReplyDelete
  15. Amazing issues here. I'm very glad to peer your post. Thanks so much and I am having a look forward to contact you. Will you please drop me a e-mail?

    Also visit my blog :: Diet Plans

    ReplyDelete