0

我目前正在尝试将一些数据从 SQL Server 2014 导出到 XML 文档。在这个项目的早期,我从这里得到了帮助,对此我非常感谢。

目前,数据的结构正确并且应该是正确的,但不幸的是,接收 XML 文档的服务器(Totalview 服务器)对它非常挑剔。SQL Server 在文档顶部添加一个图章,如下所示:

XML_F52E2B61-18A1-11d1-B105-00805F49916B

由于 XML 文档中有此标记,Totalview 服务器无法加载该文件。我查看了很多谷歌和微软的帮助页面,但找不到任何关于此的内容,也许我使用了错误的词或寻找错误的地方,这就是为什么我在这里与你们这些好人一起提问。

我想要的是这个邮票被这个邮票取代:

<?xml version="1.0"?>

它是如何完成的并不重要,我已经考虑过制作某种脚本,在 SQL Server 输出 XML 文件后改变这一点,但是让 SQL Server 首先正确输出它会很好,这样就更少了可能失败的步骤,但这是唯一的方法吗?

提前致以诚挚的问候和感谢,对于这个问题中的任何错误,我深表歉意,我对这个网站还是很陌生。

编辑

SQL查询如下:

SELECT
    [ctID],
    [forecastData/date/day] = Day(dDate),
    [forecastData/date/month] = month(dDate),
    [forecastData/date/year] = year(dDate),
    cast(([co_forecast]) as decimal(20,2)) AS [forecastData/dailyData/contactsReceived],
    cast(([AHT_Forecast]) as int) AS [forecastData/dailyData/averageAHT]
FROM 
    [ProductionForecast].[dbo].[vwForecastXMLDaily]
WHERE
    dDate BETWEEN cast(getdate() as date) AND cast(getdate()+31 as date)
GROUP BY
    [CTID], [dDate], [co_forecast], [AHT_Forecast]
FOR XML PATH ('ctForecast'), ROOT ('forecastImport')

表结构如下:

CTID    dDate   CO_Forecast AHT_Forecast
2   2016-01-15  167.75515   419.042510
2   2016-01-16  0.00000     0.000000
2   2016-01-17  0.00000     0.000000
2   2016-01-18  246.15381   382.407540
2   2016-01-19  238.35609   379.404340
2   2016-01-20  227.36992   444.473690
2   2016-01-21  232.43770   424.452350
2   2016-01-22  203.65597   403.429950
2   2016-01-23  0.00000     0.000000
2   2016-01-24  0.00000     0.000000
4

3 回答 3

0

你可以试试这样的。请记住,您导出的 XML 文件需要一个根元素,并且您需要执行 xp_cmdshell 的权限。感谢如何将 XML 查询结果保存到文件中

DECLARE @xml AS XML = (SELECT * FROM Table FOR XML PATH, ROOT('MyRoot')
)

-- Cast the XML variable into a VARCHAR
DECLARE @xmlChar AS VARCHAR(max) = CAST(@xml AS VARCHAR(max))

-- Escape the < and > characters
SET @xmlChar = REPLACE(REPLACE(@xmlChar, '>', '^>'), '<', '^<')

-- Create command text to echo to file
DECLARE @command VARCHAR(8000) = 'echo ' + @xmlChar + ' > c:\test.xml'

-- Execute the command
EXEC xp_cmdshell @command
于 2016-01-14T13:31:34.283 回答
0

到目前为止,没有任何技巧可以添加外部处理指令

我在这里问了类似的问题:SQL Server FOR XML PATH: Set xml-declaration or processing instruction "xml-stylesheet" on top

只是玩这些例子:

--If you create XML like this, a column header is created:
SELECT TOP 5 *
FROM sys.objects
FOR XML PATH('');

--You get the same result, but with your own column name (or even without, if you leave it away)
SELECT 
    (
    SELECT TOP 5 *
    FROM sys.objects
    FOR XML PATH(''),TYPE
    ) AS abcd

--without the ",TYPE" the result is no XML anymore but simple string
SELECT 
    (
    SELECT TOP 5 *
    FROM sys.objects
    FOR XML PATH('') 
    );

--you might just add your prefix with the XML 
--In this case there is no column name generated
SELECT '<?xml version="1.0"?>'+
    (
    SELECT TOP 5 *
    FROM sys.objects
    FOR XML PATH('')
    );

现在 - 已经走到了这一步 - 你的解决方案可能是这样的:

SELECT '<?xml version="1.0"?>'+
    (
    SELECT
     [ctID]
    ,[forecastData/date/day] = Day(dDate) 
    ,[forecastData/date/month] = month(dDate)
    ,[forecastData/date/year] = year(dDate)
    ,cast(([co_forecast]) as decimal(20,2)) AS [forecastData/dailyData/contactsReceived]
    ,cast(([AHT_Forecast]) as int) AS [forecastData/dailyData/averageAHT]

    FROM [ProductionForecast].[dbo].[vwForecastXMLDaily]
    where dDate between cast(getdate() as date) and cast(getdate()+31 as date)

    group by [CTID], [dDate], [co_forecast], [AHT_Forecast]

    FOR XML PATH ('ctForecast'), ROOT ('forecastImport')
    );
于 2016-01-14T16:37:37.547 回答
0

这个问题的答案是在导出到文件时禁用列标题。在我的情况下,我使用的是 Microsoft SQL 2014,所以我进入工具->选项->查询结果->结果到文本,然后删除“在结果集中包含列标题”中的勾号

即使 Totalview 服务器仍然不接受输出文件,它确实删除了标记/列标题,所以我的问题在这里得到了解决。

谢谢大家的帮助。

于 2016-01-29T12:43:39.227 回答