How to export SQL data to XML file

  • Hi,

    I have a sql data need to export to xml file; I can get data in xml mode using FOR XML PATH query and results are shown in xml style. When i click on xml link its opening in new tab with xml format exactly what i need.

    But the problem is, i need to export that xml result into .xml file on my system. I tried by using "Results to file" option but unable to get what i need. Also i do not have bcp exceute permissions.

    Can anyone please suggest me how to export those results to .xml file.

    Thanks in advance!

    Kumar

  • Kumar SQL (12/14/2012)


    Hi,

    I have a sql data need to export to xml file; I can get data in xml mode using FOR XML PATH query and results are shown in xml style. When i click on xml link its opening in new tab with xml format exactly what i need.

    But the problem is, i need to export that xml result into .xml file on my system. I tried by using "Results to file" option but unable to get what i need. Also i do not have bcp exceute permissions.

    Can anyone please suggest me how to export those results to .xml file.

    Thanks in advance!

    Kumar

    Using the results to file with xml does not do exactly what you might think. Open the file with a text editor and you will see that it contains more than just the data from the query. It contains a column name, a line with a bunch of ------

    Then your xml data.

    then a line with xxx row(s) affected.

    If you run your query with the results to file option you can save it as xml but you need to open it with a text editor and remove the additional junk it throws in there. Then you should be good to go.

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

  • If you have permission to execute xp_cmdshell then you can use SQLCMD for the same.

    thanks,

    Saurabh

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply