Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Export to XML

Author  Topic 

fralo
Posting Yak Master

161 Posts

Posted - 2011-10-11 : 12:27:00
Hi,

How can I export sql results to an xml file? I have the following in place. The code generates an xml file successfully within management studio, but I would like for it to write a file to my C drive. Is there a simple addition I could make to my code for this to work?

create proc jailinfo
as
declare @xmlquery xml
set @xmlquery = (select top 10 * from jailbook for xml auto, root('AddBookingRecord'), ELEMENTS)
select @xmlquery
go

Thanks for your help.

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-10-11 : 12:32:38
why not use in bcp and export to xml file

use the query out option


http://msdn.microsoft.com/en-us/library/ms162802.aspx

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

fralo
Posting Yak Master

161 Posts

Posted - 2011-10-11 : 12:43:59
Well I already had this in place. Plus, the bcp command comes with so many parameters (e.g. -c, -t) that I became frustrated.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-10-11 : 13:06:43
you just need to use query out option using the above query and provide output file path.

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

fralo
Posting Yak Master

161 Posts

Posted - 2011-10-11 : 13:24:22
Like this?

bcp "SELECT * FROM jailbook" queryout c:\sample.xml

It gives 'Incorrect syntax near 'queryout'.

Appreciate your help and patience.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-10-11 : 13:58:27
you need to use it with xp_cmdshell as its a command line utility

see

http://beyondrelational.com/blogs/madhivanan/archive/2008/10/10/export-to-excel-with-column-names.aspx

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -