Monday, October 9, 2017

Generated KML file from SQL query save to local drive

Leave a Comment

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.

3 Answers

Answers 1

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

Answers 2

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

    enter image description here

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

    enter image description here

  4. Then where to save the data:

    enter image description here

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

    enter image description here

  6. Past the query:

    enter image description here

  7. Then we have some settings, click finish.

    enter image description here

Answers 3

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(); 
If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment