Monday, May 21, 2012

SSIS to XML Destination

I pieced together this solution using comments on other blogs. No one had put it together quite like this, so I decided to post my solution. It works great, is fast and free.

First, write your query in SQL Server Management Studio (SSMS).

Mine was:

select * from[vwAPI-CustomerMaster]
for xml path('CustomerMaster'),root('Customers')

Your  "path" will be the top level object name (one for each row), Your"root" will be the name of the root. If you don't understand this, put in anything and run the file and you will see the values among the first bracketed ( < > ) values.

Here's a sample dummy test of Path and Root:

select * from [vwAPI-CustomerMaster]
for xml path('TweedleDee'),root('TweedleDumb')

 Outputs:

<TweedleDumb>
  <TweedleDee>
    <CUTM>ZTEMPL03<CUTM>
    <CONO>100<CONO>
    <LNCD>GB<LNCD>
    <CUNO>10000000<CUNO>
    <CUNM>M&M Distribution(APMS)<CUNM>
    <CUA1>Unit 11, The Lawn<CUA1>
    <CUA2>Mountmassey<CUA2>
    <CUA3>Macroom<CUA3>
            and so on...
So tweak your sql in SSMS until you get the output you need there. 

Tip: When SSMS gives your results it will look like a single field in a single row. Click on that lonely field and SSMS will let you see your full output.

You can also start with something like this:

select * from [vwAPI-CustomerMaster]
 for xml auto

And use that output if that works for you.
Once you have your source query, go to your Control Flow and pull in an Execute SQL task. Configure the Execute SQL task as shown below.

(click the graphic to see the whole thing)
1) Set the ResultSet to XML.
2) Set the Connection to a connection you've already created. The connection should point to wherever your data for the XML is being stored.
3) In SQL Statement, paste the code you developed in SSMS.
4) Go to the ResultSet Tab.
5) Add a Result.
6) Name it 0 [zero] - not sure why you have to name it that, but you do.
7) Tell the task where to put the Result. In my example, I created a package level variable and called it XMLDataSet. The variable is type STRING (suprised me).
8) Exit out - back to the Control Flow.
On the control flow, add a script task.
The script will be in VB and it is very simple.
1) Set the ScriptLanguage to Visual Basic. If you forget to do this first, the option gets locked down and then you have to delete the script task and create a new one.
2) Add your variable to the ReadOnlyVariables list.
3) Click on the Edit Script button.
4) Scroll down until you see:
  Public Sub Main()      
   '
   
' Add your code here
   
'
   
Dts.TaskResult = ScriptResults.Success 
End Sub
5) Replace that code with this:
Public Sub Main()       
 Dim sw As New IO.StreamWriter("C:\Temp\test.xml")      
 sw.Write(Dts.Variables("User::XMLDataSet").Value.ToString()) 
 sw.Dispose()
 D
ts.TaskResult = ScriptResults.Success
End Sub
6) Replace the two RED values with your own output location and your own variable name. (You can also convert your hard-coded output location to a variable value later).
7) Go to the Menu bar and click on the Build menu. A single item should appear as a candidate for the build action. Choose that. If you have any syntax errors, fix them.
8) Exit out of the Script dialog and return to the Control Flow.

9) Test.