[英]Sql Server FOR XML Fields and Attributes
I need to get an XML from a query:我需要从查询中获取 XML:
SELECT
... join ...
FOR XML PATH ('parent-lines'), ROOT('main-tag'), ELEMENTS XSINIL;
I have been able to get this structure:我已经能够得到这个结构:
<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>
Now I have two problems:现在我有两个问题:
<child-name />
<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>
Can you help me?你能帮助我吗?
I solved the problems, the first thanks to @Larnu and his comment, the second using this technique:我解决了这些问题,首先感谢@Larnu 和他的评论,其次是使用这种技术:
SELECT
(SELECT 'Cip' AS 'Cip' FOR XML PATH (''), TYPE) AS Header,
(SELECT 'Ciop' AS 'Ciop' FOR XML PATH ('Parent-lines'), TYPE)
FOR XML PATH (''), ROOT('main-tag');
In the first nested "Select", in the PATH I have not entered anything but on the outside I baptized it with the name I wanted to obtain "AS Header".在第一个嵌套的“Select”中,在 PATH 中我没有输入任何内容,但在外部我用我想要获得“AS Header”的名称对其进行了洗礼。 In the second "Select", however, within the PATH I entered the name I wanted to repeat for each "Parent-lines".
然而,在第二个“选择”中,在 PATH 中,我输入了我想为每个“父行”重复的名称。
I hope this question / answer will be useful to other users.我希望这个问题/答案对其他用户有用。 Thanks again and good luck to all!
再次感谢,祝大家好运!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.