views:

419

answers:

4

Hi,

I am working on an application where I need to get the SQL response as XML into an XML file (and to store it in some physical location, say c:\xyz.xml).

I am able to generate the XML content using the provisions available in SQL Server as shown below.

    SELECT * FROM @Table FOR XML AUTO, ELEMENTS

where: @Table is a table variable.

I want to know how I can store the query output to a XML file from SQL Server itself.

Thank you

A: 

If you press

ctrl + shift + f

you will have selected "Results To File." This can be found in the Query menu on the top bar of Sql Management Studio.

Or put something like this into your sql script

exec xp_cmdshell 'bcp "select * from suppliers" queryout "c:\suppliers.txt" -S server -T'

See this link, there is an issue about whether it is the app's c drive or the sql server's c drive. Have fun sorting that out.

Mark Dickinson
A: 

You can create CLR function that create the file, build it into the sql server, and use it from a stored procedure

Another way( I haven't tested it ) - There is a tool bcp

bcp "Select * from dbo..table FOR XML RAW" queryout c:\temp\test.xml -Soc-db -Uuser -Ppassword

This example is from here

Svetlozar Angelov
A: 

You need to use xp_cmdshell, and the bcp utility in the following way

EXEC xp_cmdshell 'bcp "SELECT * FROM @Table FOR XML AUTO, ELEMENTS" queryout "C:\table.xml" -c -T'

Hit me back in the comments if you've got any questions or want to know anything more about how this works.

Gausie
You'll need to specify catalog and schema for the table though (i.e. @Table should be of the format [database].dbo.[table])
Gausie
+1  A: 

You can't write to the file system from SQL Server itself. At least not that easily. There are three alternatives:

  • use xp_cmdshell. I would strongly advise against it. By default xp_cmdshell is disabled for security purposes, and to have it enabled it just for this operation opens a way to big security hole in your system.

  • use the FileSystemObject and the OLE Automation procedures sp_OACreate/sp_OAMethod. See Reading and Writing Files in SQL Server using T-SQL. This, while marginally better than the xp_cmdshell option, it doesn't give a much better security story. The only reason why is better than xp_cmdshell is that is by far less known by hackers. But the OLE Automation procedures option in SQL Server is also disabled by default and enabling this option exposes the very same security problems xp_cmdshell has.

  • use a CLR procedure. This would be my recommendation. Create an assembly with digital signature, use the assembly signature to allow, through Transact-SQL code signing, EXTERNAL ACCESS, then use the CLR procedure to write the XML into the file system. While this is significantly more complex than the simple xp_cmdshell or OLE Automation options, it is the most controlable and granular one from a security point of view and is the easiest to maintain and get right (is .Net code as opposed to shell scripts). Unfortunately, by default the clr option is also disabled in the server and has to be enabled.

Remus Rusanu