从SQL Server中的两个表生成XML

时间:2017-08-30 13:10:52

标签: sql sql-server xml

有人可以帮助我从两个sql表生成XML吗?

这就是我想要的:

<Sales>
  <Sale>
    <Journal_Prime>400000</Journal_Prime>
    <DocNumber>100001</DocNumber>
    <Details>
        <Detail>
          <Account>700300</Account>
          <Amount>276,79</Amount>
          <DebCre>-1</DebCre>
          <Ventil>70</Ventil>
          <Ref>WD2093E0V0</Ref>
          <DocNumber>100001</DocNumber>
        </Detail>
        <Detail>
          <Account>708000</Account>
          <Amount>0,00</Amount>
          <DebCre>1</DebCre>
          <Ventil>70</Ventil>
          <Ref>Korting</Ref>
          <DocNumber>100001</DocNumber>
        </Detail>
        <Detail>
          <Account>700530</Account>
          <Amount>55,00</Amount>
          <DebCre>-1</DebCre>
          <Ventil>70</Ventil>
          <Ref>Transport</Ref>
          <DocNumber>100001</DocNumber>
        </Detail>
        <Detail>
          <Account>451000</Account>
          <Amount>0,00</Amount>
          <DebCre>-1</DebCre>
          <Ventil>11</Ventil>
          <Ref>BTW</Ref>
          <DocNumber>100001</DocNumber>
        </Detail>
    </Details>
  </Sale>
</Sales>

这是我的尝试

SELECT Sale.Journal_Prime, Sale.DocNumber, Detail.Account, Detail.Account, Detail.Amount, Detail.DebCre, Detail.Ventil, Detail.Ref, Detail.DocNumber
FROM XML_FAKAdres2017  as Sale 

INNER JOIN XML_FAK2017 as Detail
ON Sale.DocNumber = Detail.DocNumber

FOR XML AUTO, ROOT('Sales'), ELEMENTS

给我这个结果

<Sales>
  <Sale>
    <Journal_Prime>400000</Journal_Prime>
    <DocNumber>100001</DocNumber>
        <Detail>
          <Account>700300</Account>
          <Amount>276,79</Amount>
          <DebCre>-1</DebCre>
          <Ventil>70</Ventil>
          <Ref>WD2093E0V0</Ref>
          <DocNumber>100001</DocNumber>
        </Detail>
        <Detail>
          <Account>708000</Account>
          <Amount>0,00</Amount>
          <DebCre>1</DebCre>
          <Ventil>70</Ventil>
          <Ref>Korting</Ref>
          <DocNumber>100001</DocNumber>
        </Detail>
        <Detail>
          <Account>700530</Account>
          <Amount>55,00</Amount>
          <DebCre>-1</DebCre>
          <Ventil>70</Ventil>
          <Ref>Transport</Ref>
          <DocNumber>100001</DocNumber>
        </Detail>
        <Detail>
          <Account>451000</Account>
          <Amount>0,00</Amount>
          <DebCre>-1</DebCre>
          <Ventil>11</Ventil>
          <Ref>BTW</Ref>
          <DocNumber>100001</DocNumber>
        </Detail>
  </Sale>
</Sales>

所以,我错过了这个代码用于导入的簿记程序所需的<Details></Details>。我不熟悉XML,说实话,我不知道这些来自何处。

感谢。 瑞克

1 个答案:

答案 0 :(得分:0)

试试这个

SELECT Sale.Journal_Prime, Sale.DocNumber , 
(SELECT Detail.Account, Detail.Account, Detail.Amount, 
Detail.DebCre, Detail.Ventil, Detail.Ref, Detail.DocNumber
FROM XML_FAK2017 as Detail where Sale.DocNumber = Detail.DocNumber
FOR XML AUTO,TYPE,ROOT('Details'),ELEMENTS)
FROM XML_FAKAdres2017  as Sale 
FOR XML AUTO, ROOT('Sales'),ELEMENTS