ssms中的值生成了xml标记

时间:2017-02-13 19:24:19

标签: sql sql-server xml ssms

我有以下代码来生成xml输出。

select
   [FirstName] as 'SubscriberName/FirstName',
   [Middle] as 'SubscriberName/MiddleName',
   LastName as 'SubscriberName/LastName',
   '123 fake street' as 'Address/Address1'
from chp_work_area.dbo.Students1099hc
FOR XML PATH('SubscriberInformation'), ROOT('Test')

这个输出是下面的xml,并且是准确的。

<Test>
  <SubscriberInformation>
    <SubscriberName>
      <FirstName>BOB</FirstName>
      <MiddleName>G</MiddleName>
      <LastName>CHOI</LastName>
    </SubscriberName>
    <Address>
      <Address1>123 fake street</Address1>
    </Address>
  </SubscriberInformation>
  <SubscriberInformation>
    <SubscriberName>
      <FirstName>MOLLY</FirstName>
      <MiddleName>G</MiddleName>
      <LastName>MORTY</LastName>
    </SubscriberName>
    <Address>
      <Address1>123 fake street</Address1>
    </Address>
  </SubscriberInformation>
</Test>

我需要的最后一部分是订阅者信息列出它在标签内的哪条记录,因此它的内容如下所示。

    <Test>
    <SubscriberInformation docID="Person001">
    <SubscriberName>
        <FirstName>BOB</FirstName>
        <MiddleName>G</MiddleName>
        <LastName>CHOI</LastName>
    </SubscriberName>
    <Address>
        <Address1>123 fake street</Address1>
    </Address>
    </SubscriberInformation>
    <SubscriberInformation docID="Person002">
    <SubscriberName>
        <FirstName>MOLLY</FirstName>
        <MiddleName>G</MiddleName>
        <LastName>MORTY</LastName>
    </SubscriberName>
    <Address>
        <Address1>123 fake street</Address1>
    </Address>
    </SubscriberInformation>
</Test>

1 个答案:

答案 0 :(得分:0)

尝试这样的事情......

SELECT ROW_NUMBER() OVER(ORDER BY [FirstName],[Middle],[LastName] ASC) AS "@docID",
   [FirstName] as 'SubscriberName/FirstName',
   [Middle] as 'SubscriberName/MiddleName',
   LastName as 'SubscriberName/LastName',
   '123 fake street' as 'Address/Address1'
from chp_work_area.dbo.Students1099hc
FOR XML PATH('SubscriberInformation'), ROOT('Test')