1

突然间,我在任何地方都找不到正确的方法或提示解决方案,所以在那里尝试:

我有一个这样的选择:

declare @XmlOutput xml 
set @XmlOutput = (
SELECT
           R.ID
          ,R.[PN] as 'Nummer'
          ,R.[TitlePrefixAbbreviation] as Title 
          ,R.[FirstName]
          ,R.LastName
          ,RA.[DescriptiveNumber]
          ,RA.[OrientationalNumber]
          ,RC.Contact  
      FROM [tbl1] as R
      left join tbl2 as RA on R.ID = RA.[RID] 
      left join [tbl3 as RC on R.ID = RC.[RID] 
      for xml auto, ROOT('mbox'), ELEMENTS
)
select @XmlOutput

结果如下所示:

<mbox>
  <R>
    <ID>66284</ID>
    <Nummer>999999</Nummer>
    <Title />
    <FirstName>test</FirstName>
    <LastName>test</LastName>
    <RA>
      <HouseNr>9999</HouseNr>
      <SequenceNr />
      <City>London</City>
      <ZIP>99999</ZIP>
      <RC>
        <Contact>letitroll@gmail.com</Contact>
      </RC>
    </RA>
  </R>
  <mbox>

如您所见,存在RA, R, RC依赖于连接表的层次结构元素。

而且我不知道如何只使用根元素<mbox>而不是子树来制作 XMLR, RC, RA

像这样的东西->

<mbox>
    <ID>66284</ID>
    <Nummer>999999</Nummer>
    <Title />
    <FirstName>Štěpánka</FirstName>
    <LastName>Solomková</LastName>
    <HouseNr>2015</HouseNr>
    <SequenceNr />
    <City>London</City>
    <ZIP>99999</ZIP>
    <Contact>letitroll@gmail.com</Contact>  
<mbox>

我考虑使用临时表的解决方法,我首先将所有选定的数据放入其中,然后从 TMP 表生成 XML,但我希望有更优雅的方法。

有人可以帮忙吗?

最好的,凝胶

4

1 回答 1

0
declare @XmlOutput xml 
set @XmlOutput = (
Select * from
(
SELECT
           R.ID
          ,R.[PN] as 'Nummer'
          ,R.[TitlePrefixAbbreviation] as Title 
          ,R.[FirstName]
          ,R.LastName
          ,RA.[DescriptiveNumber]
          ,RA.[OrientationalNumber]
          ,RC.Contact  
      FROM [tbl1] as R
      left join tbl2 as RA on R.ID = RA.[RID] 
      left join [tbl3 as RC on R.ID = RC.[RID] 
) mbox
      for xml auto,  ELEMENTS
)
select @XmlOutput
于 2013-06-10T08:16:00.193 回答