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 ))
  3. go
  4. create table Album(AlbumID int primary key not null,
  5. ArtistID int not null,
  6. AlbumName ) not null,
  7. YearReleased smallint not null)
  8. go
  9. ,'Pink Floyd')
  10. ,'Incubus')
  11. ,'Prince')
  12. ,,)
  13. ,,)
  14. ,,)
  15. ,,)
  16. ,,)
  17. ,,)
  18. ,,)
 

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. select @XmlOutput
  8. 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

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

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

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. Imports System
  5. Imports System.Data
  6. Imports System.Math
  7. Imports Microsoft.SqlServer.Dts.Runtime
  8. Public Class ScriptMain
  9. Public Sub Main()
  10. '
  11. ' Add your code here
  12. '
  13. Dim XMLString As String = " "
  14. XMLString = Dts.Variables("XMLOutput").Value.ToString.Replace("<ROOT>", "").Replace("</ROOT>", "")
  15. XMLString = "<?xml version=""1.0"" ?>" + XMLString
  16. GenerateXmlFile("C:\\MusicCollection.xml", XMLString)
  17. End Sub
  18. Public Sub GenerateXmlFile(ByVal filePath As String, ByVal fileContents As String)
  19. Dim objStreamWriter As IO.StreamWriter
  20. Try
  21. objStreamWriter = New IO.StreamWriter(filePath)
  22. objStreamWriter.Write(fileContents)
  23. objStreamWriter.Close()
  24. Catch Excep As Exception
  25. MsgBox(Excep.Message)
  26. End Try
  27. Dts.TaskResult = Dts.Results.Success
  28. End Sub
  29. 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. Imports System
  5. Imports System.Data
  6. Imports System.Math
  7. Imports Microsoft.SqlServer.Dts.Runtime
  8. <System.AddIn.AddIn("ScriptMain", Version:="1.0", Publisher:="", Description:="")> _
  9. <System.CLSCompliantAttribute(False)> _
  10. Partial Public Class ScriptMain
  11. Inherits Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase
  12. Enum ScriptResults
  13. Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success
  14. Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure
  15. End Enum
  16. Public Sub Main()
  17. '
  18. ' Add your code here
  19. '
  20. Dim XMLString As String = " "
  21. XMLString = Dts.Variables("XMLOutput").Value.ToString.Replace("<ROOT>", "").Replace("</ROOT>", "")
  22. XMLString = "<?xml version=""1.0"" ?>" + XMLString
  23. GenerateXmlFile("C:\\MusicCollection.xml", XMLString)
  24. End Sub
  25. Public Sub GenerateXmlFile(ByVal filePath As String, ByVal fileContents As String)
  26. Dim objStreamWriter As IO.StreamWriter
  27. Try
  28. objStreamWriter = New IO.StreamWriter(filePath)
  29. objStreamWriter.Write(fileContents)
  30. objStreamWriter.Close()
  31. Catch Excep As Exception
  32. MsgBox(Excep.Message)
  33. End Try
  34. Dts.TaskResult = ScriptResults.Success
  35. End Sub
  36. 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.

最新文章

  1. EXCEL设置选中单元格样式
  2. &lt;&lt;你最喜欢做什么--兴趣问题清单&gt;&gt;总结
  3. Perl重命名当前目录下的文件
  4. EasyUI组件(窗口组件)
  5. Python+Selenium进行UI自动化测试项目中,常用的小技巧4:日志打印,longging模块(控制台和文件同时输出)
  6. sphinx增量索引
  7. Ubuntu 14.04 部署 CEPH集群
  8. Chrome开发者工具详解(1):Elements、Console、Sources面板
  9. 加载php_curl.dll和php_openssl.dll出错原因及解决办法
  10. Oracle Semaphore Management in UNIX Administration
  11. Eclipse编辑器样式修改
  12. 显示hibernate的sql语句
  13. HTML标签自定义属性
  14. git代码回滚
  15. win2012 配置wamp的若干错误
  16. update_engine-整体结构(三)
  17. 新手详解JAVA+数据库+JSP完成简单页面
  18. Java开源内容管理CMS系统J4CMS集成到JTM
  19. metal的gpu query
  20. 利用Serv-U搭建FTP服务器

热门文章

  1. React.js学习之理解JSX和组件
  2. bzoj 1864
  3. visible, disable, css绑定
  4. jquery的一个模板引擎-zt
  5. scrollview和viewpager滑动冲突
  6. SQL必知必会 -------- 通配符、计算字段、函数
  7. [Codeforces995C]Leaving the Bar 瞎搞
  8. quartz定时任务,已过期的内容自动下线
  9. C# 找出实现某个接口的所有类
  10. OpenVAS漏洞扫描基础教程之创建用户组与创建角色