Click here to Skip to main content
15,887,446 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am getting Xml output in sql . now how to save this result to my desktop as xml file eg abc.xml
I tried using cmdshell to create xml file but its giving error

SQL
select @cmd = '
    bcp "select * from #temp_table row for xml auto, root(''rows''), elements" ' +
   'queryout "C:\Users\ss\Desktop\XML\sample.xml" -S  -c -r -t';
exec xp_cmdshell @cmd



Can you tell me simple way to put sql xml to file.xml?
Posted
Comments
Sergey Alexandrovich Kryukov 22-Oct-12 18:14pm    
XML? To desktop? (Sigh...)
Aha, and if the user is "ll" instead of "xx", write new "program"?
--SA

1 solution

you can try something like:

bcp "select * from dbname.dbo.tablename" queryout "D:\Temp\SampleFile.txt" -T -c -S "Server"

and if you want the columns names to be shown as first row in your file you will need to UNION the column names to your main query, for example:



bcp "select 'c1' as c1, 'c2' as c2 union select cast(c1 as varchar) as c1, cast(c2 as varchar) from dbname.dbo.tablename" queryout "D:\Temp\SampleFile.txt" -T -c -S "Server";
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900