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

13

我的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'))

我想将查询结果保存为.XML文件到本地驱动器,请给予建议。


1
这是一个重复的问题:如何从SQL Server 2008生成XML文件 - TT.
它缺少一些上下文。是什么触发了这个查询?一个输出给用户下载的Web应用程序?一个定期作业,将单个查询参数输出存储到FS中?还是一个遍历一系列参数并为每个参数抓取输出文件的计划作业?或者是遍历一系列参数生成单个文件的其他方式? - Marcus Vinicius Pompeu
3个回答

5
不是最优雅的方法,但可以使用 批量复制程序xp_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

您可以运行以下命令:

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

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

Microsoft文档有关xp_cmdshell的信息,请单击此处,有关bcp的信息请参见此处


3

为了将远程查询的结果保存到本地文件中,您可以使用像这个示例一样的Powershell脚本:

$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();

通过将脚本保存到扩展名为".ps1"的文件中,并使用类似以下命令的命令,可以从命令提示符中执行脚本:

powershell -ExecutionPolicy RemoteSigned -File "C:\PowershellScripts\ExampleExport.ps1"

可以使用 Windows 任务计划程序中的任务进行安排,以自动化导出。 或者,使用 SQL Server agent 作业安排一个 Powershell 或 CmdExec 步骤。


嗨,我们如何执行这个脚本? - user1046415
@user1046415,我在我的答案中添加了方法。 - Dan Guzman

3
是处理大型数据集时的绝佳选择。另外,您也可以尝试使用SQL Management Studio - Export Data
以下为具体操作步骤:
  1. 右击数据库名称,选择Tasks, 再选择Export Data

  2. 点击Next

    enter image description here

  3. 选择SQL Server Native Client、sql server、数据库名称和验证方法:

    enter image description here

  4. 选择数据保存位置:

    enter image description here

  5. 指定获取数据方式(在此处输入查询语句):

    enter image description here

  6. 粘贴查询语句:

    enter image description here

  7. 选择一些设置后,点击完成finish

    enter image description here


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接