简体   繁体   中英

Generated KML file from SQL query save to local drive

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.

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

Using bcp is definite choice especially when working with large data sets. Alternatively, you can try using SQL Management Studio - Export Data .

  1. Open the interface - Right Click on database name, then Tasks , then Export Data
  2. The menu is opened. Click Next

    在此输入图像描述

  3. Then choose SQL Server Native Client , sql server, database name and authentication method:

    在此输入图像描述

  4. Then where to save the data:

    在此输入图像描述

  5. Then how we are getting the data (in your case SQL query):

    在此输入图像描述

  6. Past the query:

    在此输入图像描述

  7. Then we have some settings, click finish .

    在此输入图像描述

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM