Zerotoinfinity
Zerotoinfinity

Reputation: 6540

How to export from SQL Server to XML

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()

Upvotes: 3

Views: 19046

Answers (2)

billinkc
billinkc

Reputation: 61269

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.

Upvotes: 1

Yuck
Yuck

Reputation: 50865

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

Upvotes: 5

Related Questions