SQL 导出时的服务器戳 XML
SQL Server stamp on exporting FOR XML
我目前正在尝试将一些数据从 SQL Server 2014 导出到 XML 文档。我早些时候在这个项目上得到了这里的帮助,对此我非常感谢。
目前数据的结构正确且符合预期,但不幸的是,接收 XML 文档的服务器(Totalview 服务器)对此非常挑剔。 SQL 服务器正在文档顶部添加图章,如下所示:
XML_F52E2B61-18A1-11d1-B105-00805F49916B
由于 XML 文档中的这个标记,Totalview 服务器无法加载该文件。我看了很多 google 和 Microsoft 的帮助页面,但找不到任何相关信息,也许我用错了词或看错了地方,这就是为什么我在这里和你们这些好人一起问。
我想要的是用这个邮票代替这个邮票:
<?xml version="1.0"?>
它是如何完成的并不重要,我想过制作某种脚本来在 SQL 服务器输出 XML 文件后改变它,但如果能得到 SQL 服务器首先正确输出它,这样可能失败的步骤更少,但这是唯一的方法吗?
在此先致以诚挚的问候和感谢,对于这个问题中的任何错误,我深表歉意,我对这个网站还很陌生。
编辑
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')
Table结构如下:
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
你可以尝试这样的事情。请记住,您导出的 XML 文件需要一个根元素,并且您需要权限才能执行 xp_cmdshell。感谢How To Save XML Query Results to a File
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
目前为止还没有添加外部处理指令的技巧
我在这里问了类似的问题:
试一下这些例子:
--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')
);
这个问题的答案是在导出到文件时禁用列 header。在我的例子中,我使用的是 Microsoft SQL 2014,所以我进入工具->选项->查询结果->结果到文本,然后删除 'Include Column headers in the result set'
中的勾号
即使 Totalview 服务器仍然不接受输出文件,它确实删除了 stamp/column header 所以我的问题就解决了。
谢谢大家的帮助。
我目前正在尝试将一些数据从 SQL Server 2014 导出到 XML 文档。我早些时候在这个项目上得到了这里的帮助,对此我非常感谢。
目前数据的结构正确且符合预期,但不幸的是,接收 XML 文档的服务器(Totalview 服务器)对此非常挑剔。 SQL 服务器正在文档顶部添加图章,如下所示:
XML_F52E2B61-18A1-11d1-B105-00805F49916B
由于 XML 文档中的这个标记,Totalview 服务器无法加载该文件。我看了很多 google 和 Microsoft 的帮助页面,但找不到任何相关信息,也许我用错了词或看错了地方,这就是为什么我在这里和你们这些好人一起问。
我想要的是用这个邮票代替这个邮票:
<?xml version="1.0"?>
它是如何完成的并不重要,我想过制作某种脚本来在 SQL 服务器输出 XML 文件后改变它,但如果能得到 SQL 服务器首先正确输出它,这样可能失败的步骤更少,但这是唯一的方法吗?
在此先致以诚挚的问候和感谢,对于这个问题中的任何错误,我深表歉意,我对这个网站还很陌生。
编辑
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')
Table结构如下:
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
你可以尝试这样的事情。请记住,您导出的 XML 文件需要一个根元素,并且您需要权限才能执行 xp_cmdshell。感谢How To Save XML Query Results to a File
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
目前为止还没有添加外部处理指令的技巧
我在这里问了类似的问题:
试一下这些例子:
--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')
);
这个问题的答案是在导出到文件时禁用列 header。在我的例子中,我使用的是 Microsoft SQL 2014,所以我进入工具->选项->查询结果->结果到文本,然后删除 'Include Column headers in the result set'
中的勾号即使 Totalview 服务器仍然不接受输出文件,它确实删除了 stamp/column header 所以我的问题就解决了。
谢谢大家的帮助。