How to export from SQL Server to XML

2019-03-31 11:51发布

What is the easiest way to export to XML from SQL Server [Table or view] to XML?

This is what I have done till now

Execute Sql Task

SELECT * FROM Production.Product 
FOR XML AUTO, TYPE, ROOT('Data')

ResultSet XML

in left result set section I have created new variable 0 User::XMLVal

Script Task

 Dim sw As New IO.StreamWriter("D:\Apps\SSIS\test.xml")
    sw.Write(Dts.Variables("User::XMLVal").Value.ToString())


    sw.Dispose()

2条回答
男人必须洒脱
2楼-- · 2019-03-31 12:26

You can also use the query in a data flow source adapter and use the Export Column Transformation --- Using SSIS to extract a XML representation of table data to a file

This saves you the trouble of managing your own scripts and what not, it's all out of the box functionality.

查看更多
Root(大扎)
3楼-- · 2019-03-31 12:35

If you want to use the task components, set up an Execute SQL Task to read the result of a SQL statement or stored procedure into a user-defined SSIS variable. Your statement above is a good example of what it should look like:

enter image description here

Then use the XML Task to write the contents of the variable to a file:

enter image description here

查看更多
登录 后发表回答