Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/83.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 使用FOR XML子句时如何保存XML输出?_Sql_Xml_Tsql - Fatal编程技术网

Sql 使用FOR XML子句时如何保存XML输出?

Sql 使用FOR XML子句时如何保存XML输出?,sql,xml,tsql,Sql,Xml,Tsql,因此,我试图将SQL查询结果保存为XML文件。我已经让我的查询结果输出了我想要的XML,但我最终将使用它作为存储过程,因此我需要能够将结果输出为XML文件 我已尝试使用xp\u cmdshell方法 declare @cmd nvarchar(255); select @cmd = ' bcp " Select ( Select GetDate() AS [ApplicationArea/CreationDateTime], BODID.BODID AS [Applicatio

因此,我试图将SQL查询结果保存为XML文件。我已经让我的查询结果输出了我想要的XML,但我最终将使用它作为存储过程,因此我需要能够将结果输出为XML文件

我已尝试使用
xp\u cmdshell
方法

declare @cmd nvarchar(255);

select @cmd = ' bcp " Select
    (
    Select GetDate() AS [ApplicationArea/CreationDateTime], BODID.BODID AS [ApplicationArea/BODID]
    From BODID
    Where BODID = "ME-COIL-MESSAGE"
    For XML Path(''), Type
    ),
    (
    Select 
        (
        Select pp.EquipmentID AS [Location/EquipmentID], pp.EquipmentElementLevel AS [Location/EquipmentElementLevel] , GetDate() AS [PublishedDate]
        From ProductionPerformance AS pp
        For XML Path(''), Root("ProductionPerformance"), Type
        ),
        (
        Select ProductionResponse.ID,
            (
            Select Sample.MaterialLotID AS [ID],
                (
                Select MaterialLotID, ID AS [MaterialActualProperty/ID], ValueString AS [MaterialActualProperty/Value/ValueString] 
                From Sample
                For XML Path(''), Root("MaterialActual") , Type
                )
            From Sample
            Where MaterialLotID is not null 
            For XML Path(''), Root("SegmentResponse"), Type
            )
        From ProductionResponse
        For XML Path(''), Root("ProductionResponse"), Type
        )
    For XML Path(''), Root("DataArea"), Type
    )
For XML Path(''), Root("SyncProductionPerformance"), Type" ' + 'queryout "C:\Users\spakuresa\Documents\Out\sample.xml" -S YOUR_INSTANCE_NAME -T -w -r -t -x';

exec xp_cmdshell @cmd
go
结果是这样,没有保存任何文件。。。

要将数据保存到xml文件,请启用SQLCMD模式,然后在脚本顶部添加“:out”参数


至于“传递表”,只需在“FOR XML PATH('SyncProductionPerformance')之前添加from子句即可。

只需在FOR XML之前添加from[table_name]。我还建议你自己做一些“谷歌搜索”作业。像这样的谷歌查询应该会给你足够的答案:它并没有按照我所希望的方式进行格式化,但确实有效!谢谢