【发布时间】:2021-08-26 12:03:43
【问题描述】:
我需要从查询中获取 XML:
SELECT
... join ...
FOR XML PATH ('parent-lines'), ROOT('main-tag'), ELEMENTS XSINIL;
我已经能够得到这个结构:
<main-tag>
<parent-lines>
...
<child-name>EXAMPLE</child-name> //case when the child is popolated
<child-name xsi:nil=true /> //case when the child is empty
...
</parent-lines>
...many parent-lines
</main-tag>
现在我有两个问题:
- 首先是让子名称不带 xsi: nil 属性,以防它们为空,因此:
<child-name />
- 第二个是我有某种“标题”,它是静态的,适用于任何父标记,我想将其插入到我的 XML 结构中,并获得与此类似的结构:
<main-tag>
<header>
...
<child-name>A sort of explain of the field</child-name>
...
</header> //single header
<parent-lines>...</parent-lines> //many parent-lines
...
</main-tag>
你能帮帮我吗?
【问题讨论】:
-
SQL Server 不会为
NULL值创建 XML 节点。您必须使用空字符串作为值,它会显示为<child-name></child-name>(虽然外观不同,但在语法上与<child-name/>相同)。 -
@Larnu 非常感谢,现在好多了!
-
在提出问题时,您需要提供一个可重现的最小示例:(1) DDL 和样本数据填充,即 CREATE 表加上 INSERT、T-SQL 语句。 (2) 你需要做什么,即逻辑,以及你在 T-SQL 中尝试实现它。 (3) 基于上述#1 中的样本数据的期望输出。 (4) 你的 SQL Server 版本 (SELECT @@version;)
标签: sql sql-server xml tsql export-to-xml