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
   3:
   4: -- --------------------------------------------------------
   5: -- Enable xp_cmdshell
   6: -- --------------------------------------------------------
   7:
   8: -- Allow advanced options to be changed.
   9: EXEC sp_configure 'show advanced options', 1
  10: GO
  11:
  12: -- Update the currently configured value for advanced options.
  13: RECONFIGURE
  14: GO
  15:
  16: -- Enable xp_cmdshell
  17: EXEC sp_configure 'xp_cmdshell', 1
  18: GO
  19:
  20: -- Update the currently configured value for xp_cmdshell
  21: RECONFIGURE
  22: GO
  23:
  24: -- Disallow further advanced options to be changed.
  25: EXEC sp_configure 'show advanced options', 0
  26: GO
  27:
  28: -- Update the currently configured value for advanced options.
  29: RECONFIGURE
  30: GO
  31:
  32:
  33: -- --------------------------------------------------------
  34: -- Sample export via bcp
  35: -- --------------------------------------------------------
  36: DECLARE @FileName VARCHAR(50)
  37: DECLARE @SQLCmd   VARCHAR(500)
  38:
  39: SELECT  @FileName = 'C:TempSampleXMLOutput.xml'
  40:
  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
  49:
  50: -- display command, for visual  check
  51: SELECT @SQLCmd AS 'Command to execute'
  52:
  53: -- create the XML file
  54: EXECUTE master..xp_cmdshell @SQLCmd
  55:
  56:
VN:F [1.9.22_1171]
Rating: 8.3/10 (28 votes cast)
VN:F [1.9.22_1171]
Rating: +3 (from 5 votes)
SQLXML : How To Save XML Query Results to a File Using BCP, 8.3 out of 10 based on 28 ratings  
Be Sociable, Share!
  • Tweet