Login or Sign Up to become a member!
LessThanDot Sit Logo

LessThanDot

Data Management

Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary. Once you register for an account you will have immediate access to the forums and all past articles and commentaries.

LTD Social Sitings

Lessthandot twitter Lessthandot Linkedin Lessthandot friendfeed Lessthandot facebook Lessthandot rss

Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.

Your profile

Search

XML Feeds

Google Ads

« SQL Friday, The Best SQL Server Links Of The Past Week Episode 14SQL Friday, The Best SQL Server Links Of The Past Week Episode 13 »
comments
Rate Post:
submit to reddit Digg!FacebookDotnetkicks

So you want to spit out some XML from SQL Server into a file, how can you do that? There are a couple of ways, I will show you how you can do it with SSIS. In the SSIS package you need an Execute SQL Task and a Script Task.

Let's get started

First create and populate these two tables in your database

  1. CREATE TABLE Artist (ArtistID INT PRIMARY KEY not null,
  2. ArtistName VARCHAR(38))
  3. go
  4.  
  5. CREATE TABLE Album(AlbumID INT PRIMARY KEY not null,
  6. ArtistID INT not null,
  7. AlbumName VARCHAR(100) not null,
  8. YearReleased SMALLINT not null)
  9. go
  10.  
  11.  
  12. INSERT INTO Artist VALUES(1,'Pink Floyd')
  13. INSERT INTO Artist VALUES(2,'Incubus')
  14. INSERT INTO Artist VALUES(3,'Prince')
  15.  
  16. INSERT INTO Album VALUES(1,1,'Wish You Were Here',1975)
  17. INSERT INTO Album VALUES(2,1,'The Wall',1979)
  18.  
  19.  
  20.  
  21. INSERT INTO Album VALUES(3,3,'Purple Rain',1984)
  22. INSERT INTO Album VALUES(4,3,'Lotusflow3r',2009)
  23. INSERT INTO Album VALUES(5,3,'1999',1982)
  24.  
  25.  
  26. INSERT INTO Album VALUES(6,2,'Morning View',2001)
  27. INSERT INTO Album VALUES(7,2,'Light Grenades',2006)

Now create this proc

  1. CREATE PROC prMusicCollectionXML
  2. AS
  3. DECLARE @XmlOutput xml
  4. SET @XmlOutput = (SELECT ArtistName,AlbumName,YearReleased FROM Album
  5. join Artist ON Album.ArtistID = Artist.ArtistID
  6. FOR XML AUTO, ROOT('MusicCollection'), ELEMENTS)
  7.  
  8. SELECT @XmlOutput
  9. go

After executing the proc

  1. EXEC prMusicCollectionXML

you will see the following output

  1. <MusicCollection>
  2.  <Artist>
  3.   <ArtistName>Pink Floyd</ArtistName>
  4.  <Album>
  5.   <AlbumName>Wish You Were Here</AlbumName>
  6.   <YearReleased>1975</YearReleased>
  7.   </Album>
  8.  <Album>
  9.   <AlbumName>The Wall</AlbumName>
  10.   <YearReleased>1979</YearReleased>
  11.   </Album>
  12.   </Artist>
  13.  <Artist>
  14.   <ArtistName>Prince</ArtistName>
  15.  <Album>
  16.   <AlbumName>Purple Rain</AlbumName>
  17.   <YearReleased>1984</YearReleased>
  18.   </Album>
  19.  <Album>
  20.   <AlbumName>Lotusflow3r</AlbumName>
  21.   <YearReleased>2009</YearReleased>
  22.   </Album>
  23.  <Album>
  24.   <AlbumName>1999</AlbumName>
  25.   <YearReleased>1982</YearReleased>
  26.   </Album>
  27.   </Artist>
  28.  <Artist>
  29.   <ArtistName>Incubus</ArtistName>
  30.  <Album>
  31.   <AlbumName>Morning View</AlbumName>
  32.   <YearReleased>2001</YearReleased>
  33.   </Album>
  34.  <Album>
  35.   <AlbumName>Light Grenades</AlbumName>
  36.   <YearReleased>2006</YearReleased>
  37.   </Album>
  38.   </Artist>
  39.   </MusicCollection>

So far so good, so how do we dump that data into a file?
Create a new SSIS package add an ADO.NET Connection, name it AdventureWorksConnection
Drop an Execute SQL Task onto your control flow and modify the properties so it looks like this

Execute SQL Task

On the add a result set by clicking on the add button, change the variable name to User::XMLOutput if it is not already like that

Note!!! In SSIS 2008 this variable should be already created otherwise it will fail

Execute SQL Task Adding A Resultset

Now execute the package.
You will be greeted with the following message:
Error: 0xC00291E3 at Execute SQL Task, Execute SQL Task: The result binding name must be set to zero for full result set and XML results.
Task failed: Execute SQL Task
In order to fix that, change the Result Name property from NewresultName to 0, now run it again and it should execute successfully.

Our next step will be to write this XML to a file.
Add a Script Task to the package,double click the Script Task,click on script and type XMLOutput into the property of ReadWriteVariables. It should look like the image below

SSIS Script Task

Click the Design Script button, this will open up a code window, replace all the code you see with this

  1. ' Microsoft SQL Server Integration Services Script Task
  2. ' Write scripts using Microsoft Visual Basic
  3. ' The ScriptMain class is the entry point of the Script Task.
  4.  
  5. Imports System
  6. Imports System.Data
  7. Imports System.Math
  8. Imports Microsoft.SqlServer.Dts.Runtime
  9.  
  10.  
  11. Public Class ScriptMain
  12.  
  13.    
  14.  
  15.     Public Sub Main()
  16.         '
  17.         ' Add your code here
  18.         '
  19.         Dim XMLString As String = " "
  20.  
  21.  
  22.  
  23.         XMLString = Dts.Variables("XMLOutput").Value.ToString.Replace("<ROOT>", "").Replace("</ROOT>", "")
  24.         XMLString = "<?xml version=""1.0"" ?>" + XMLString
  25.  
  26.         GenerateXmlFile("C:\\MusicCollection.xml", XMLString)
  27.  
  28.     End Sub
  29.  
  30.     Public Sub GenerateXmlFile(ByVal filePath As String, ByVal fileContents As String)
  31.  
  32.         Dim objStreamWriter As IO.StreamWriter
  33.         Try
  34.  
  35.             objStreamWriter = New IO.StreamWriter(filePath)
  36.  
  37.             objStreamWriter.Write(fileContents)
  38.  
  39.             objStreamWriter.Close()
  40.  
  41.         Catch Excep As Exception
  42.  
  43.             MsgBox(Excep.Message)
  44.  
  45.         End Try
  46.         Dts.TaskResult = Dts.Results.Success
  47.     End Sub
  48.  
  49. End Class

SSIS 2008 requires a code change
Here is what the code should look like if you are running SSIS 2008

  1. ' Microsoft SQL Server Integration Services Script Task
  2. ' Write scripts using Microsoft Visual Basic 2008.
  3. ' The ScriptMain is the entry point class of the script.
  4.  
  5. Imports System
  6. Imports System.Data
  7. Imports System.Math
  8. Imports Microsoft.SqlServer.Dts.Runtime
  9.  
  10. <System.AddIn.AddIn("ScriptMain", Version:="1.0", Publisher:="", Description:="")> _
  11. <System.CLSCompliantAttribute(False)> _
  12. Partial Public Class ScriptMain
  13.     Inherits Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase
  14.  
  15.     Enum ScriptResults
  16.         Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success
  17.         Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure
  18.     End Enum
  19.    
  20.  
  21.    
  22.  
  23.     Public Sub Main()
  24.         '
  25.         ' Add your code here
  26.         '
  27.         Dim XMLString As String = " "
  28.  
  29.  
  30.  
  31.         XMLString = Dts.Variables("XMLOutput").Value.ToString.Replace("<ROOT>", "").Replace("</ROOT>", "")
  32.         XMLString = "<?xml version=""1.0"" ?>" + XMLString
  33.  
  34.         GenerateXmlFile("C:\\MusicCollection.xml", XMLString)
  35.  
  36.     End Sub
  37.  
  38.     Public Sub GenerateXmlFile(ByVal filePath As String, ByVal fileContents As String)
  39.  
  40.         Dim objStreamWriter As IO.StreamWriter
  41.         Try
  42.  
  43.             objStreamWriter = New IO.StreamWriter(filePath)
  44.  
  45.             objStreamWriter.Write(fileContents)
  46.  
  47.             objStreamWriter.Close()
  48.  
  49.         Catch Excep As Exception
  50.  
  51.             MsgBox(Excep.Message)
  52.  
  53.         End Try
  54.  
  55.         Dts.TaskResult = ScriptResults.Success
  56.  
  57.  
  58.     End Sub
  59.  
  60. End Class

There are a couple of things you need to know, the XML will be generated inside a <ROOT> tag, I am stripping that out on line 23 of the code, on line 24 I am adding <?xml version="1.0" ?> to the file. Line 26 has the location where the file will be written, right now it is C:\MusicCollection.xml but you can modify that.

So now we are all done with this. It is time to run this package. Run the package and you should see that file has been created.

About the Author

User bio imageDenis has been working with SQL Server since version 6.5. Although he worked as an ASP/JSP/ColdFusion developer before the dot com bust, he has been working exclusively as a database developer/architect since 2002. In addition to English, Denis is also fluent in Croatian and Dutch, but he can curse in many other languages and dialects (just ask the SQL optimizer) He lives in Princeton, NJ with his wife and three kids.
Social SitingsTwitterFacebookLinkedInHomePageLTD RSS Feed
7010 views
submit to reddit Digg!FacebookDotnetkicks

Comments and Feedback

19 comments

Comment from: rpf [Visitor]
*****
Works like a wonder! Thank you sir!
04/22/09 @ 01:02
Comment from: Reuben [Visitor]
*****
Nice Job! Great Post!
06/26/09 @ 09:41
Comment from: NetGolf [Visitor]
*****
Great post! However, I got "out of memory" error when I tried to create an XML from a table with 3 million rows. Due to security concern, I was not able to use "bcp". What else can I do? Please help! Thanks!!
10/12/09 @ 15:12
Comment from: Charles Marche [Visitor] Email · http://www.acmecontracts.com
I managed to get this to work on SQL 2005. But the script has many problems when running on SQL 2008. Any chance of posting a SQL 2008 version please???

Thanks.
11/27/09 @ 08:32
Comment from: SQLDenis [Member] Email
Charles,

what errors are you getting? I will run this on sql 2008 later today and will let you know if it works
11/27/09 @ 08:59
Comment from: SQLDenis [Member] Email
Charles I added the 2008 code changes
11/29/09 @ 18:32
Comment from: Charles [Visitor]
Hi SQLDenis,

Many thanks for the new code! It seems to execute as long as you keep the entrypoint to Main.

The output file has problems, it only contains 1 line which says xml version="1.0". Then the rest of the file is blank.

A couple of questions please,
1) In 2008 it doesn't create the Result Set entry for User:XMLOutput automatically. Is it meant to auto-detect this? from an output from the Stored Procedure?

2) As a solution it seems complicated for just outputing an XML file. I'd heard great things about SSIS, but it seems extremely complex - probably rightly so.
11/30/09 @ 19:02
Comment from: StanleyG [Visitor]
Thanks for sharing. This was helpful. What determines the encoding (e.g. UTF-8, UTF-16) of the final output file using this process? Can one specify what they want the encoding to be?
12/13/09 @ 20:26
Comment from: Ted Krueger (onpnt) [Member] Email
You'll need to set it in the script task using Encoding
http://msdn.microsoft.com/en-us/library/ms143456.aspx
12/14/09 @ 06:20
Comment from: StanleyG [Visitor]
Thanks for the response, onpnt. I will review.
12/15/09 @ 00:43
Comment from: Tony [Visitor]
Found this article after much searching on Google. It is sad that there isn't a native component for writing out a file within SSIS, but at least the solution is a very simple script. Thanks for this!
01/13/10 @ 11:19
Comment from: Greca [Visitor]
This article was very useful for me, especially the trick on line 23 …. Thank you very much

Greca
01/18/10 @ 01:23
Comment from: Slick [Visitor] Email
How can I date stamp the file name?

musiccollection_04212010.xml or similar?

thanks,
s
04/28/10 @ 12:38
Comment from: SQLDenis [Member] Email
Slick,

Like this


Dim FileName As String
FileName = "musiccollection_" & Today.ToString("MMddyyyy") & ".xml"


and then instead of this

GenerateXmlFile("C:\\MusicCollection.xml", XMLString)

do this

GenerateXmlFile(FileName , XMLString)
04/28/10 @ 12:58
Comment from: Slick [Visitor] Email
Thanks for your reply.

Sorry I'm weak with programming languages.

Thank you so much again.

S
04/28/10 @ 13:35
Comment from: Gerry [Visitor] Email
Wow. thanks this is great.

How do I now call these packages I have created to run on a schedule?
So that the XML populates in a folder everyday or every hour?
05/21/10 @ 14:55
Comment from: SQLDenis [Member] Email
Gerry, you can create a SQL Agent job and schedule that or start the job from a procedure/SQL with sp_start_job
05/22/10 @ 04:29
Comment from: Sainis [Visitor] Email
Hey,Can anyone please help me on how can I do the same task using OLEDB Connection instead of ADO.Net.
06/09/10 @ 21:14
Comment from: CVN [Visitor] Email
Hey,

This works great. Thanks a lot.

Can somebody tell me how to add a namespace?

Example:
ns0:MusicCollection xmlns:ns0="http://ABC.Data.AX.v01"

Thanks in advance,
CVN
07/08/10 @ 09:04

Leave a comment


Your email address will not be revealed on this site.

Your URL will be displayed.
(Line breaks become <br />)
(Name, email & website)
(Allow users to contact you through a message form (your email will not be revealed.)