[英]SQL Server : creating XML with child nodes for non unique values
I am having a hard time figuring out how to create an XML file from a SQL select
that has sub nodes to store the not unique values. 我很难弄清楚如何从具有子节点来存储非唯一值的SQL select
中创建XML文件。
I am using Microsoft SQL Server 2012 我正在使用Microsoft SQL Server 2012
eg 例如
select company, founder
from companies
The result of the query is: 查询的结果是:
microsoft corp Bill Gates
microsoft corp Paul Allen
apple inc Steve Jobs
apple inc Ronald Wayne
apple inc Steve Wozniak
I would like to generate an XML like 我想生成一个XML
<values>
<company>microsoft corp</company>
<founders>
<founder id="1">Bill Gates</founder>
<founder id="2">Paul Allen</founder>
</founders>
</values>
<values>
<company>apple inc</company>
<founders>
<founder id="1">Steve Jobs</founder>
<founder id="2">Ronald Wayne</founder>
<founder id="3">Steve Wozniak</founder>
</founders>
</company>
</values>
I am not sure the node <founders>
is required, I think it would also work fine for me to have the founders directly under the <values>
node, since they keep in the right company, getting an id to have a list. 我不确定是否需要节点<founders>
,我认为将创始人直接放在<values>
节点下对我来说也很好,因为他们会留在正确的公司中,并获得ID以包含列表。
What I get right now, using FOR XML
, then I tried different options is the following: 我现在使用FOR XML
,然后尝试了以下不同的选项:
<values>
<company>apple inc</company>
<founder>Steve Jobs</founder>
</values>
<values>
<company>apple inc</company>
<founder>Ronald Wayne</founder>
</values>
<values>
<company>apple inc</company>
<founder>Steve Wozniak</founder>
</values>
which is not compatible with what I need to achieve then. 这与我当时要实现的目标不兼容。
Any help to get all the founders under the same <values>
node is highly appreciated. 非常感谢所有将所有创建者置于同一<values>
节点下的帮助。
Thanks! 谢谢!
Searched a bit internet and found solution: 搜索了一下互联网,找到了解决方案:
DECLARE @table TABLE (company VARCHAR(100), founder VARCHAR(100))
insert into @table SELECT 'microsoft corp', 'Bill Gates'
insert into @table SELECT 'microsoft corp', 'Paul Allen'
insert into @table SELECT 'apple inc', 'Steve Jobs'
insert into @table SELECT 'apple inc', 'Ronald Wayne'
insert into @table SELECT 'apple inc', 'Steve Wozniak'
SELECT company,
(
SELECT ROW_NUMBER()OVER( ORDER BY founder ) as 'founder/@id',
founder as 'founder'
FROM @table as F
WHERE F.company = Comp.company
FOR XML PATH(''), TYPE
) AS founders
FROM @table as Comp
GROUP BY company
FOR XML PATH('VALUES')
Result: 结果:
<VALUES>
<company>apple inc</company>
<founders>
<founder id="1">Ronald Wayne</founder>
<founder id="2">Steve Jobs</founder>
<founder id="3">Steve Wozniak</founder>
</founders>
</VALUES>
<VALUES>
<company>microsoft corp</company>
<founders>
<founder id="1">Bill Gates</founder>
<founder id="2">Paul Allen</founder>
</founders>
</VALUES>
Test Data 测试数据
DECLARE @Companies TABLE (company VARCHAR(100), founder VARCHAR(100))
INSERT INTO @Companies VALUES
('microsoft corp', 'Bill Gates'),
('microsoft corp', 'Paul Allen'),
('apple inc', 'Steve Jobs'),
('apple inc', 'Ronald Wayne'),
('apple inc', 'Steve Wozniak')
Query 询问
SELECT company
,(SELECT ROW_NUMBER() OVER (PARTITION BY Company ORDER BY Founder ASC) AS [@ID]
,B.founder [text()]
FROM @companies B
WHERE B.company = A.company
FOR XML PATH('Founder'),TYPE) AS Founders
FROM @companies A
GROUP BY A.Company
FOR XML PATH('values'),ROOT('Doc'), ELEMENTS
OUTPUT 输出值
<Doc>
<values>
<company>apple inc</company>
<Founders>
<Founder ID="1">Ronald Wayne</Founder>
<Founder ID="2">Steve Jobs</Founder>
<Founder ID="3">Steve Wozniak</Founder>
</Founders>
</values>
<values>
<company>microsoft corp</company>
<Founders>
<Founder ID="1">Bill Gates</Founder>
<Founder ID="2">Paul Allen</Founder>
</Founders>
</values>
</Doc>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.