My SQL Query generates a XML output:
select 'TEST.kml' as name,
(select 'TEST' as name, (
select (
select top 10 issue as name,
null as description,
null as 'Point/coordinates',
(
select
null as altitudeMode,
Coordinates as 'coordinates'
for xml path('Polygon'), type)
from Mapping for xml path('Placemark'), type))
for xml path ('Line') , type)
for xml path ('Doc'), root('kml'))
I want to save the output of the query as .XML file on to local drive.Please advise.
Method Number 1 – Copy Grid results and paste into Excel After ensuring results to grid turned on, Execute your query, right-click the top left-hand corner of the results grid. Right-click on the database you want to export from. Then Select tasks and “Export Data”.
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.
Not the most elegant way but it is possible to use bulk copy program
and xp_cmdshell
to do this. Few things first, xp_cmdshell
is blocked by default by SQL Server as part of the security configuration so you will need to enable that first and BCP
requires you to have access to the directory that you want to create the file.
To enable xp_cmdshell
you'll need run sp_configure
and RECONFIGURE
, use this:
EXEC sp_configure'xp_cmdshell', 1
RECONFIGURE
GO
EXEC sp_configure 'show advanced options', 1
RECONFIGURE
GO
Then you can run the following:
EXEC xp_cmdshell 'bcp "SELECT * FROM [Database].dbo.[Table] FOR XML AUTO,
ELEMENTS" queryout "C:\test.xml" -c -T'
Just add your query into it and make sure you add []
around your table names.
The Microsoft Documents for xp_cmdshell are here and bcp can be found here
To save the results of a remote query to a local file, you could use a Powershell script like this example:
$connection = New-Object System.Data.SqlClient.SqlConnection("Data Source=YourServer;Initial Catalog=YourDatabase;Integrated Security=SSPI")
$command = New-Object System.Data.SqlClient.SqlCommand(@("
select 'TEST.kml' as name,
(select 'TEST' as name, (
select (
select top 10 issue as name,
null as description,
null as 'Point/coordinates',
(
select
null as altitudeMode,
Coordinates as 'coordinates'
for xml path('Polygon'), type)
from Mapping for xml path('Placemark'), type))
for xml path ('Line') , type)
for xml path ('Doc'), root('kml');"), $connection);
$connection.Open();
$command.ExecuteScalar() | Out-File -FilePath "C:\KmlFiles\YourFile.kml";
$connection.Close();
The script can be executed from a command prompt by saving the script to a file with a ".ps1" extension and using a command like:
powershell -ExecutionPolicy RemoteSigned -File "C:\PowershellScripts\ExampleExport.ps1"
This command can be scheduled using a Windows Task Scheduler task to automate the export. Alternatively, schedule using a SQL Server agent job with a Powershell or CmdExec step.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With