从SQL查询生成的KML文件保存到本地驱动器

use*_*415 12 xml sql sql-server kml sql-server-2008

我的SQL查询生成XML输出:

         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'))
Run Code Online (Sandbox Code Playgroud)

我想将查询的输出保存为.XML文件到本地驱动器.请指教.

dba*_*jtr 5

不是最优雅的方式,但可以使用bulk copy programxp_cmdshell执行此操作.前几件事,xp_cmdshell阻止在默认情况下由SQL Server作为部分安全配置,所以你将需要启用第一,BCP需要你有访问您要创建的文件的目录.

为了使xp_cmdshell你需要运行sp_configureRECONFIGURE使用这样的:

EXEC sp_configure'xp_cmdshell', 1
RECONFIGURE
GO
EXEC sp_configure 'show advanced options', 1
RECONFIGURE
GO
Run Code Online (Sandbox Code Playgroud)

然后您可以运行以下命令:

EXEC xp_cmdshell 'bcp "SELECT * FROM [Database].dbo.[Table] FOR XML AUTO,
ELEMENTS" queryout "C:\test.xml" -c -T'
Run Code Online (Sandbox Code Playgroud)

只需将您的查询添加到其中,并确保添加[]您的表名称.

xp_cmdshell 的Microsoft Documents这里,可以在这里找到bcp