我在 SQL Server 中有两个具有一对多关系的表。我需要跨这些表执行 XQuery,以获得 xml 输出
表格(和样本记录)是:
实体_A
ID Name Description Value1 Value2
------------------------------------------------
10 aName1 aDescription1 AttVal1 AttVal2
11 aName2 aDescription2 AttVal21 AttVal25
实体_B
ID ID_Entity_A SubValue
---------------------------
1 10 sv1
2 10 sv2
3 10 sv3
4 10 sv4
5 11 sv5
6 11 sv6
我想获得执行 xquery 的输出是这样的:
<MyRoot>
<EntityNode>
<Identification>
<Name>aName1</Name>
<Description>aDescription1</Description>
</Identification>
<EntityAttributes>
<Value1>AttVal1</Value1>
<Value2>AttVal2</Value2>
<SubValue>sv1<SubValue>
<SubValue>sv2<SubValue>
<SubValue>sv3<SubValue>
<SubValue>sv4<SubValue>
</EntityAttributes>
</EntityNode>
<EntityNode>
<Identification>
<Name>aName2</Name>
<Description>aDescription2</Description>
</Identification>
<EntityAttributes>
<Value1>AttVal21</Value1>
<Value2>AttVal25</Value2>
<SubValue>sv5<SubValue>
<SubValue>sv6<SubValue>
</EntityAttributes>
</EntityNode>
...
</MyRoot>
这是我运行的 xquery:
SELECT
Name AS 'Identification/Name',
Description AS 'Identification/Description',
Value1 AS 'EntityAttributes/Value1',
Value2 AS 'EntityAttributes/Value2',
(
SELECT
SubValue AS SubValue
FROM
Entity_B
WHERE
Entity_B.ID_Entity_A = Entity_A.ID
FOR XML PATH(''), ROOT('EntityAttributes'), TYPE)
FROM Entity_A
FOR XML PATH('EntityNode'), ROOT('MyRoot'), ELEMENTS
但是我获得的输出是这样的(与所需的输出不同):
<MyRoot>
<EntityNode>
<Identification>
<Name>aName1</Name>
<Description>aDescription1</Description>
</Identification>
<EntityAttributes>
<Value1>AttVal1</Value1>
<Value2>AttVal2</Value2>
</EntityAttributes>
<EntityAttributes>
<SubValue>sv1<SubValue>
<SubValue>sv2<SubValue>
<SubValue>sv3<SubValue>
<SubValue>sv4<SubValue>
</EntityAttributes>
</EntityNode>
<EntityNode>
<Identification>
<Name>aName2</Name>
<Description>aDescription2</Description>
</Identification>
<EntityAttributes>
<Value1>AttVal21</Value1>
<Value2>AttVal25</Value2>
</EntityAttributes>
<EntityAttributes>
<SubValue>sv5<SubValue>
<SubValue>sv6<SubValue>
</EntityAttributes>
</EntityNode>
...
</MyRoot>
有什么建议吗?
此致