我对XML进行SQL查询,如下所示(SQL Server 2012):
Use MedStreaming
SELECT distinct 1 as Tag,
NULL as Parent,
NULL AS [ExamTypes!1],
NULL as [ExamType!2!ExamTypeID],
NULL as [Template!3!ShortString],
NULL as [Template!3!Order]
UNION ALL
SELECT distinct 2 as Tag,
1 as Parent,
NULL,
Temp_Type.ExamTypeId as [ExamType!2!ExamTypeID],
NULL as [Template!3!ShortString],
NULL as [Template!3!Order]
FROM LayoutSectionTemplate_ExamType AS Temp_Type
INNER JOIN LayoutSection_Template as temp
ON Temp_Type.TemplateId = temp.TemplateId
UNION ALL
SELECT 3 as Tag,
2 as Parent,
NULL,
Temp_Type.ExamTypeId,
temp.ShortString,
Temp_Type.[Order]
FROM LayoutSectionTemplate_ExamType AS Temp_Type
INNER JOIN LayoutSection_Template as temp
ON Temp_Type.TemplateId = temp.TemplateId
ORDER BY [ExamType!2!ExamTypeID],[Template!3!Order]
FOR XML EXPLICIT
我尝试使用以下命令调用它并将XML结果写入XML文件:
sqlcmd -i "%~dp0\MedStreaming.Util\Scripts\GetExamTypes-TemplatesRelations.sql" -o "C:\temp\examTypes-reportsTemplates.xml"
但是文件结果XML格式不正确,并删除了一些文本。 我试着把#" XML打开"在查询的第一个,但它不起作用(它给我语法错误)。
任何帮助?
答案 0 :(得分:0)
这是annoying limitation of SQLCMD
interface(与Invoke-SqlCmd
Powershell模块相同) - 你必须把 XML 或 JSON < / strong>输出到NVARCHAR(MAX)
或XML
数据类型,以便SQLCMD
可以正确处理输出。 SQLCMD
输出有 2MB行大小阈值,然后开始将输出数据分块为其他DataRows
。
注意: 您还应该设置MaxCharLength
if the data exceeds 4K characters in size。
DECLARE @XML as XML
SET @XML= (select Name from TestXML For XML path ('')
DECLARE @json NVARCHAR(MAX)
SELECT @json = (select * from mytable for json auto)
select * from openjson(@json)
另一个选择是use bcp
utility which outputs directly to disk用于更大的数据转储。