1

我在 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>

有什么建议吗?

此致

4

1 回答 1

1

使用列别名而不是 指定子查询的元素名称root()。这将构成Value1Value2最终在与SubValue's 相同的节点中。

SQL小提琴

MS SQL Server 2008 架构设置

create table Entity_A
(
  ID int,
  Name varchar(10),
  Description varchar(20),
  Value1 varchar(10),
  Value2 varchar(10)
);

create table Entity_B
(
  ID int,
  ID_Entity_A int,
  SubValue char(3)
);

insert into Entity_A values
(10,  'aName1',   'aDescription1',   'AttVal1',    'AttVal2'),
(11,  'aName2',   'aDescription2',   'AttVal21',   'AttVal25');

insert into Entity_B values
(1,      10,         'sv1'),
(2,      10,         'sv2'),
(3,      10,         'sv3'),
(4,      10,         'sv4'),
(5,      11,         'sv5'),
(6,      11,         'sv6');

查询 1

select A.Name as [Identification/Name],
       A.Description as [Identification/Description],
       A.Value1 as [EntityAttribute/Value1],
       A.Value2 as [EntityAttribute/Value2],
       (
       select B.SubValue
       from dbo.Entity_B as B
       where A.ID = B.ID_Entity_A
       for xml path(''), type
       ) as [EntityAttribute]
from dbo.Entity_A as A
for xml path('EntityNode'), root('MyRoot');

结果

<MyRoot>
  <EntityNode>
    <Identification>
      <Name>aName1</Name>
      <Description>aDescription1</Description>
    </Identification>
    <EntityAttribute>
      <Value1>AttVal1</Value1>
      <Value2>AttVal2</Value2>
      <SubValue>sv1</SubValue>
      <SubValue>sv2</SubValue>
      <SubValue>sv3</SubValue>
      <SubValue>sv4</SubValue>
    </EntityAttribute>
  </EntityNode>
  <EntityNode>
    <Identification>
      <Name>aName2</Name>
      <Description>aDescription2</Description>
    </Identification>
    <EntityAttribute>
      <Value1>AttVal21</Value1>
      <Value2>AttVal25</Value2>
      <SubValue>sv5</SubValue>
      <SubValue>sv6</SubValue>
    </EntityAttribute>
  </EntityNode>
</MyRoot>    |                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
于 2013-05-16T05:45:37.640 回答