Create XML Files Out Of SQL Server With SSIS And FOR XML Syntax
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
- create table Artist (ArtistID int primary key not null,
- ArtistName ))
- go
- create table Album(AlbumID int primary key not null,
- ArtistID int not null,
- AlbumName ) not null,
- YearReleased smallint not null)
- go
- ,'Pink Floyd')
- ,'Incubus')
- ,'Prince')
- ,,)
- ,,)
- ,,)
- ,,)
- ,,)
- ,,)
- ,,)
Now create this proc
- create proc prMusicCollectionXML
- as
- declare @XmlOutput xml
- set @XmlOutput = (select ArtistName,AlbumName,YearReleased from Album
- join Artist on Album.ArtistID = Artist.ArtistID
- FOR XML AUTO, ROOT('MusicCollection'), ELEMENTS)
- select @XmlOutput
- go
After executing the proc
- exec prMusicCollectionXML
you will see the following output
- <MusicCollection>
- <Artist>
- <ArtistName>Pink Floyd</ArtistName>
- <Album>
- <AlbumName>Wish You Were Here</AlbumName>
- <YearReleased>1975</YearReleased>
- </Album>
- <Album>
- <AlbumName>The Wall</AlbumName>
- <YearReleased>1979</YearReleased>
- </Album>
- </Artist>
- <Artist>
- <ArtistName>Prince</ArtistName>
- <Album>
- <AlbumName>Purple Rain</AlbumName>
- <YearReleased>1984</YearReleased>
- </Album>
- <Album>
- <AlbumName>Lotusflow3r</AlbumName>
- <YearReleased>2009</YearReleased>
- </Album>
- <Album>
- <AlbumName>1999</AlbumName>
- <YearReleased>1982</YearReleased>
- </Album>
- </Artist>
- <Artist>
- <ArtistName>Incubus</ArtistName>
- <Album>
- <AlbumName>Morning View</AlbumName>
- <YearReleased>2001</YearReleased>
- </Album>
- <Album>
- <AlbumName>Light Grenades</AlbumName>
- <YearReleased>2006</YearReleased>
- </Album>
- </Artist>
- </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
- ' Microsoft SQL Server Integration Services Script Task
- ' Write scripts using Microsoft Visual Basic
- ' The ScriptMain class is the entry point of the Script Task.
- Imports System
- Imports System.Data
- Imports System.Math
- Imports Microsoft.SqlServer.Dts.Runtime
- Public Class ScriptMain
- Public Sub Main()
- '
- ' Add your code here
- '
- Dim XMLString As String = " "
- XMLString = Dts.Variables("XMLOutput").Value.ToString.Replace("<ROOT>", "").Replace("</ROOT>", "")
- XMLString = "<?xml version=""1.0"" ?>" + XMLString
- GenerateXmlFile("C:\\MusicCollection.xml", XMLString)
- End Sub
- Public Sub GenerateXmlFile(ByVal filePath As String, ByVal fileContents As String)
- Dim objStreamWriter As IO.StreamWriter
- Try
- objStreamWriter = New IO.StreamWriter(filePath)
- objStreamWriter.Write(fileContents)
- objStreamWriter.Close()
- Catch Excep As Exception
- MsgBox(Excep.Message)
- End Try
- Dts.TaskResult = Dts.Results.Success
- End Sub
- End Class
SSIS 2008 requires a code change Here is what the code should look like if you are running SSIS 2008
- ' Microsoft SQL Server Integration Services Script Task
- ' Write scripts using Microsoft Visual Basic 2008.
- ' The ScriptMain is the entry point class of the script.
- Imports System
- Imports System.Data
- Imports System.Math
- Imports Microsoft.SqlServer.Dts.Runtime
- <System.AddIn.AddIn("ScriptMain", Version:="1.0", Publisher:="", Description:="")> _
- <System.CLSCompliantAttribute(False)> _
- Partial Public Class ScriptMain
- Inherits Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase
- Enum ScriptResults
- Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success
- Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure
- End Enum
- Public Sub Main()
- '
- ' Add your code here
- '
- Dim XMLString As String = " "
- XMLString = Dts.Variables("XMLOutput").Value.ToString.Replace("<ROOT>", "").Replace("</ROOT>", "")
- XMLString = "<?xml version=""1.0"" ?>" + XMLString
- GenerateXmlFile("C:\\MusicCollection.xml", XMLString)
- End Sub
- Public Sub GenerateXmlFile(ByVal filePath As String, ByVal fileContents As String)
- Dim objStreamWriter As IO.StreamWriter
- Try
- objStreamWriter = New IO.StreamWriter(filePath)
- objStreamWriter.Write(fileContents)
- objStreamWriter.Close()
- Catch Excep As Exception
- MsgBox(Excep.Message)
- End Try
- Dts.TaskResult = ScriptResults.Success
- End Sub
- 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.
Create XML Files Out Of SQL Server With SSIS And FOR XML Syntax的更多相关文章
- SQL Server 2008中如何为XML字段建立索引
from:http://blog.csdn.net/tjvictor/article/details/4370771 SQL Server中的XML索引分为两类:主XML 索引和辅助XML索引.其中辅 ...
- 在SQL Server中将数据导出为XML和Json
有时候需要一次性将SQL Server中的数据导出给其他部门的也许进行关联或分析,这种需求对于SSIS来说当然是非常简单,但很多时候仅仅需要一次性导出这些数据而建立一个SSIS包就显得小题大做 ...
- 微软BI 之SSIS 系列 - 两种将 SQL Server 数据库数据输出成 XML 文件的方法
开篇介绍 在 SSIS 中并没有直接提供从数据源到 XML 的转换输出,Destination 的输出对象有 Excel File, Flat File, Database 等,但是并没有直接提供 X ...
- Create maintenance backup plan in SQL Server 2008 R2 using the wizard
You will need to identify how you want your maintenance plan to be setup. In this example the mainte ...
- SQL SERVER 原来还可以这样玩 FOR XML PATH
FOR XML PATH 有的人可能知道有的人可能不知道,其实它就是将查询结果集以XML形式展现,有了它我们可以简化我们的查询语句实现一些以前可能需要借助函数活存储过程来完成的工作.那么以一个实例为主 ...
- SQL Server 将数据导出为XML和Json
有时候需要一次性将SQL Server中的数据导出给其他部门的也许进行关联或分析,这种需求对于SSIS来说当然是非常简单,但很多时候仅仅需要一次性导出这些数据而建立一个SSIS包就显得小题大做,而SQ ...
- Sql Server 部署SSIS包完成远程数据传输
本篇介绍如何使用SSIS和作业完成自动更新目标数据任务. ** 温馨提示:如需转载本文,请注明内容出处.** 本文链接:https://www.cnblogs.com/grom/p/9018978.h ...
- SQL Server 2008 R2——使用FOR XML PATH实现多条信息按指定格式在一行显示
=================================版权声明================================= 版权声明:原创文章 谢绝转载 请通过右侧公告中的“联系邮 ...
- SQL SERVER与SSIS 数据类型对应关系
随机推荐
- vim编码相关
与vim编码相关的四个配置: encoding:vim核心编码,所有vim交换区,信息提示区都用这个编码.打开文件的编码如果是其他编码,会自动转换为核心编码,保存时再转回文件编码. fileencod ...
- grep 同时排除多个关键字
不说废话, 例如需要排除 abc.txt 中的 mmm nnn grep -v 'mmm\|nnn' abc.txt 再举个例子,需要确定mac 的本机ip地址, 显然直接可以输入 ifcon ...
- 实现RMQ的两种常用方法
RMQ RMQ(Range Maximum/Minimum Question)是指区间最值问题,在OI中较为常见,一般可以用ST表和线段树实现. ST表是基于倍增思想的一种打表方法,在确定区间范围和所 ...
- shell 切分文件名提取文件扩展名或提取文件名
有些脚本要根据文件名进行各种处理,有时候需要保留文件名抛弃文件后缀,也有时候需要文件后缀不要文件名,这类提取文件部分的操作使用shell的内建功能就能实现.需要用到的几个操作符有:%.%%.#.##. ...
- 顺序存储线性表_ArrayList
相信大家在日常开发过程中 List 应该使用的非常非常多,今天就来简单学习一下 List 的数据结构 顺序存储线性表. 一.什么是顺序存储线性表 顺序存储线性表是最基本.最简单.也是最常用的一种数据结 ...
- Hibernate lazy(延迟加载)
Hibernat加载策略有两种,分别为即时加载和延迟加载(懒加载或者lazy),get使用的是即时加载,使用get获取数据时会立即查找(会先去缓存查找,如果缓存中没有找到,才会去数据库中查找),而lo ...
- [BZOJ 2821] 作诗
Link: BZOJ 2821 传送门 Solution: 一道类似区间众数的经典分块 由于个数为偶数这样的条件不能支持快速合并 因此要先$O(n*sqrt(n))$预处理出$pre[i][j]$表示 ...
- [转]android中listview点击事件失效
首先说明一下我想实现的功能: 点击某个item之后,让其颜色发生变化.如果变化网上有很多例子,我就不班门弄斧了.Listview之所以点击没有反应是因为上图中绿色部分(自己定义的一个继承BaseAda ...
- 对于GTPv1协议头部的解析
参考ETSI EN 301 347 GTP是GPRS Tunnelling Protocol 的简称.GTP分为GTPv0(已经淘汰),GTPv1 和GTPv2.下面,介绍的是GTPv1. GTPv1 ...
- Bayesian Face Revisited A Joint Formulation
很有意思的一篇人脸识别算法文章,人家写的太好,就不好意思写了,收集了一些资料,包括了原理介绍,流程图,项目网址和作者主页信息等. 参考资料: [1]. http://blog.csdn.net/csy ...