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()
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.
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:Then use the
XML Task
to write the contents of the variable to a file: