SQLXML : How To Save XML Query Results to a File Using BCP

One way to save your XML query results to the file system is by using bcp (bulk copy program).

Be aware of the following before deciding to use bcp for your regular export requirements:

  • bcp is a program external to SSMS. If you need to use this from within your scripts, you will need to enable xp_cmdshell. xp_cmdshell is an extended stored procedure that allows external command line processes to be executed from within SQL Server. Enabling xp_cmdshell is considered to be a big no no in terms of security because this opens up avenues for malicious attacks through SQL Server.
  • Depending on how much data you need to export, you may need to batch your export to overcome rowsize limitations of bcp.

If you intend to use bcp from within SSMS, you will need to enable xp_cmdshell first, otherwise you will get the following error:

Msg 15281, Level 16, State 1, Procedure xp_cmdshell, Line 1 SQL Server blocked access to procedure ‘sys.xp_cmdshell’ of component ‘xp_cmdshell’ because this component is turned off as part of the security configuration for this server. A system administrator can enable the use of ‘xp_cmdshell’ by using sp_configure. For more information about enabling ‘xp_cmdshell’, see “Surface Area Configuration” in SQL Server Books Online.

The following example walks you through enabling xp_cmdshell, and using bcp from within SSMS to save your XML query to an external file.

   1: USE AdventureWorks
   2: GO
   4: -- --------------------------------------------------------
   5: -- Enable xp_cmdshell
   6: -- --------------------------------------------------------
   8: -- Allow advanced options to be changed.
   9: EXEC sp_configure 'show advanced options', 1
  10: GO
  12: -- Update the currently configured value for advanced options.
  14: GO
  16: -- Enable xp_cmdshell
  17: EXEC sp_configure 'xp_cmdshell', 1
  18: GO
  20: -- Update the currently configured value for xp_cmdshell
  22: GO
  24: -- Disallow further advanced options to be changed.
  25: EXEC sp_configure 'show advanced options', 0
  26: GO
  28: -- Update the currently configured value for advanced options.
  30: GO
  33: -- --------------------------------------------------------
  34: -- Sample export via bcp
  35: -- --------------------------------------------------------
  36: DECLARE @FileName VARCHAR(50)
  37: DECLARE @SQLCmd   VARCHAR(500)
  39: SELECT  @FileName = 'C:TempSampleXMLOutput.xml'
  41: -- in this command, we are making sure there is only one ROOT node
  42: SELECT  @SQLCmd = 'bcp ' +
  43:                         '"SELECT Resume  ' +
  44:                         ' FROM AdventureWorks.HumanResources.JobCandidate ' +
  45:                         ' FOR XML PATH(''''), ROOT(''HRResumes''), TYPE "' +
  46:                   ' queryout '  +
  47:                   @FileName +
  48:                   ' -w -T -S' + @@SERVERNAME
  50: -- display command, for visual  check
  51: SELECT @SQLCmd AS 'Command to execute'
  53: -- create the XML file
  54: EXECUTE master..xp_cmdshell @SQLCmd

11 thoughts on “SQLXML : How To Save XML Query Results to a File Using BCP

  1. Sergei April 23, 2009 / 4:21 pm

    Thank you! It is very useful (at least to me).


  2. John May 29, 2009 / 11:33 am

    Less is more. Besides being short, sweet, and very elegant I use the little script in processing my XML almost every day.

    Keep it up Belle!


    • belle May 29, 2009 / 11:55 am

      Thanks John! 🙂 Happy to hear that!


  3. emime July 2, 2009 / 9:01 am

    Thanks, it’s useful and worked for me.
    Just a note:
    * the -S option is required only in case of remote execution of bpc. I had to cut it.
    * VARCHAR(500) may be too much limiting


  4. emime July 3, 2009 / 2:13 am

    I want to add a very important note. With bigger result sets I got a line brakes (and consequently the stored xml corrupted). The only thing that solved the problem was to add the options -r -t without argument. This instructs bps to override newline (default n) and tab (default t) with a null character (that is your final xml will not have line brakes or tabs but will be well formed!!!).
    Furthermore, you can use batch to fix the limit size. Here the final instruction fragment for the bcp I used:

    SELECT @SQLCmd = 'bcp ... queryout ... -w -r -t -b 4000'


  5. Andrew July 16, 2009 / 7:35 pm

    This is very cool … thanks so much for posting this.


  6. Lydia May 20, 2010 / 8:44 am

    I got error:
    Error = [Microsoft][SQL Native Client][SQL Server]Login failed for user ‘NT AUTHORITYSYSTEM’.
    I checked the DB , it has user ‘NT AUTHORITYSYSTEM’


  7. Shawn in NC February 24, 2011 / 2:58 pm

    Thanks, Belle! I had everything going right, could read the XML in SSMS but not a browser. Needed the little word ‘TYPE’. Your example saved my day, but I still need to go back and learn what in the world ‘TYPE’ had to do with my XML failing.


  8. Sean May 6, 2011 / 3:48 am

    The output from mine returned the following and did not create the file, as expected.

    Starting copy…
    1 rows copied.
    Network packet size (bytes): 4096
    Clock Time (ms.) Total : 1 Average : (1000.00 rows per sec.)

    The BCP line reads as follows:

    bcp “SELECT * FROM mytable FOR XML PATH(”), ROOT(‘DATA’), TYPE ” queryout C:datatest1.xml -w -T -Smyserver

    I executed the query part which produced valid XML



  9. Akash December 8, 2012 / 6:14 am

    file is not saving in .xml format
    i am getting record in this way.



  10. Ron Fowler January 22, 2013 / 8:57 am

    Many thanks! I’ve been beating my head against a stone
    wall for three days, but this post just solved my problems.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s