SQL 至 XML 数据传输

SQL to XML transfer of data

好的,过去几天我一直在努力学习 SQL 到 XML,这就是我到目前为止能够自学的东西。

`SELECT distinct StudentItem.foldername AS "foldername", StudentItem.status, StudentItem.vhrid, StudentItem.firstname, StudentItem.middleinitial, StudentItem.lastname,
dbo.getEnumDescript(StudentType, 'StudentType') AS title,
StudentItem.email, 
dbo.getEnumDescript(OfficeLocation, 'OfficeLocation') AS Office,
practices.id as 'StudentItem/practices/practice/id',
practices.name as 'StudentItem/practices/practice/name',
schoolItem.Name as 'StudentItem/bio/schools/schoolItem/schoolname',
schoolItem.schoolYear as 'lawyerItem/bio/schools/schoolItem/schoolyear'
FROM [dbo].[Student] as lawyerItem
LEFT JOIN [dbo].[StudentGroups] as aprac on StudentItem.vhrid = aprac.vhrid
INNER JOIN [dbo].[PracticeGroups] as practices on aprac.PracticeGroupID = practices.ID
LEFT JOIN [dbo].[StudentEducation] as schoolItem on StudentItem.vhrid = schoolItem.vhrid
where StudentItem.vhrid='50330'
FOR XML path, ROOT ('StudentItem'), ELEMENTS;`  

我得到的是这个

`<StudentItems>
  <row>
    <foldername>susan.wissink</foldername>
    <status>1</status>
    <vhrid>50330</vhrid>
    <firstname>Susan</firstname>
    <middleinitial>M.</middleinitial>
    <lastname>Wissink</lastname>
    <title>Student leader</title>
    <email>swissink@blank.com</email>
    <Office>Phoenix</Office>
    <StudentItem>
      <practices>
        <practice>
          <id>681</id>
          <name>Real Estate Finance and Lending</name>
        </practice>
      </practices>
      <bio>
        <schools>
          <schoolItem>
            <schoolname>&lt;i&gt;Best in America®&lt;/i&gt;, ASU</schoolname>
            <schoolyear>2016</schoolyear>
          </schoolItem>
        </schools>
      </bio>
    </StudentItem>
  </row>
  <row>
    <foldername>susan.wissink</foldername>
    <status>1</status>
    <vhrid>50330</vhrid>
    <firstname>Susan</firstname>
    <middleinitial>M.</middleinitial>
    <lastname>Wissink</lastname>
    <title>Student leader</title>
    <email>swissink@blank.com</email>
    <Office>Phoenix</Office>
    <StudentItem>
      <practices>
        <practice>
          <id>681</id>
          <name>Real Estate Finance and Lending</name>
        </practice>
      </practices>
      <bio>
        <schools>
          <schoolItem>
            <schoolname>&lt;i&gt;Best in America®&lt;/i&gt;, UOP</schoolname>
            <schoolyear>2011-2015</schoolyear>
          </schoolItem>
        </schools>
      </bio>
    </StudentItem>
  </row>`

但我正在努力让所有的实践和学校都出现在那个人的一个条目中。或多或少,我试图让它看起来像下面这样。

`<StudentItems>
  <row>
    <foldername>susan.wissink</foldername>
    <status>1</status>
    <vhrid>50330</vhrid>
    <firstname>Susan</firstname>
    <middleinitial>M.</middleinitial>
    <lastname>Wissink</lastname>
    <title>Student leader</title>
    <email>swissink@blank.com</email>
    <Office>Phoenix</Office>
    <StudentItem>
      <practices>
        <practice>
          <id>681</id>
          <name>Real Estate Finance and Lending</name>
          <id>683</id>
          <name>Business and Finance</name>
        </practice>
      </practices>
      <bio>
        <schools>
          <schoolItem>
            <schoolname>&lt;i&gt;Best in America®&lt;/i&gt;, UOP</schoolname>
            <schoolyear>2011-2015</schoolyear>
            <schoolname>&lt;i&gt;Best in America®&lt;/i&gt;, ASU</schoolname>
            <schoolyear>2016</schoolyear>
          </schoolItem>
        </schools>
      </bio>
    </StudentItem>
  </row>`

欢迎任何帮助。谢谢。

没有示例数据,很难编写代码和测试。但通常您需要做的是创建子查询来创建 practiceschoolItem XML 节点。像这样:

SELECT distinct StudentItem.foldername AS "foldername", 
    StudentItem.status, 
    StudentItem.vhrid, 
    StudentItem.firstname, 
    StudentItem.middleinitial, 
    StudentItem.lastname,
    dbo.getEnumDescript(StudentType, 'StudentType') AS title,
    StudentItem.email, 
    dbo.getEnumDescript(OfficeLocation, 'OfficeLocation') AS Office,
    (
        select practices.id, practices.name
        from [dbo].[StudentGroups] as aprac
        INNER JOIN [dbo].[PracticeGroups] as practices 
        on aprac.PracticeGroupID = practices.ID 
        where StudentItem.vhrid = aprac.vhrid
        FOR XML path(''), type
    ) 'StudentItem/practices/practice',
    (
        select Name schoolname, schoolYear
        from [dbo].[StudentEducation] schoolItem
        where StudentItem.vhrid = schoolItem.vhrid
        FOR XML path(''), type
    ) 'StudentItem/bio/schools/schoolItem'
FROM [dbo].[Student] as StudentItem
where StudentItem.vhrid='50330'
FOR XML path, ROOT ('StudentItem');