如何从MS SQL创建以下XML? 我已经查看了谷歌并且无法看到我的具体示例如下,谢谢。 这将来自使用XML PATH的SQL查询。
<message>
<header date="15/07/2016" userid="QUOTEJOB">
<schema name="TKJobLoaderSchema" version="1.0" />
<source system="" product="" productversion="" />
<destination system="" product="" productversion="" />
</header>
<body>
<jobs>
<job action="jmCreate" company="02" contract="QW" description="test job" job_type="02" priority="5" created_by="QUOTEJOB">
<job_lines>
<job_line line_no="1" line_type="SOR" code="AQW" quantity="1916.5" />
</job_lines>
<job_narratives>
<job_narrative id="2" narrative="4678f874-314c-4584-99e3-c69e3af71999" />
</job_narratives>
<job_property company="02" ref="02363" />
</job>
</jobs>
</body>
</message>
答案 0 :(得分:1)
假设每个值都是1:1
,您的给定样本可以像下面这样创建(用实际的列名,变量等代替文字):
SELECT {d'2016-07-15'} AS [header/@date]
,'QUOTEJOB' AS [header/@userid]
,'TKJobLoaderSchema' AS [header/schema/@name]
,'1.0' AS [header/schema/@version]
,'' AS [header/source/@system]
,'' AS [header/source/@product]
,'' AS [header/source/@productversion]
,'' AS [header/destination/@system]
,'' AS [header/destination/@product]
,'' AS [header/destination/@productversion]
,'jmCreate' AS [body/jobs/job/@action]
,'02' AS [body/jobs/job/@company]
--more attributes of <job>
,1 AS [body/jobs/job/job_lines/job_line/@line_no]
--more attributes of <job_line>
,2 AS [body/jobs/job/job_narratives/job_narrative/@id]
--more attributes of <job_narrative>
,'02' AS [body/jobs/job/job_property/@company]
,'02363' AS [body/jobs/job/job_property/@ref]
FOR XML PATH('message')
结果
<message>
<header date="2016-07-15T00:00:00" userid="QUOTEJOB">
<schema name="TKJobLoaderSchema" version="1.0" />
<source system="" product="" productversion="" />
<destination system="" product="" productversion="" />
</header>
<body>
<jobs>
<job action="jmCreate" company="02">
<job_lines>
<job_line line_no="1" />
</job_lines>
<job_narratives>
<job_narrative id="2" />
</job_narratives>
<job_property company="02" ref="02363" />
</job>
</jobs>
</body>
</message>