从多个sql表创建xml

时间:2016-02-17 08:44:17

标签: sql xml sql-server-2008

我尝试从3个sql表创建一个xml文件,如下所示

SELECT * 
FROM approval approval,
diag diag,
ser ser
where approval.id = diag.id
and approval.id = ser.id
FOR XML AUTO, ELEMENTS,
root('Head')

我得到了o / p如下

<Head>
  <approval>
     .........
  <diag>
     ........
  <ser>
    ........
  </ser>
  </diag>
 </approval>
<Head>

我需要的o / p是。

 <Head>
      <approval>
         .........
      <diag>
         ........
      </diag>
      <ser>
        ........
      </ser>
     </approval>
    <Head>

如何实现同样的目标。

1 个答案:

答案 0 :(得分:0)

我得到了答案。

SELECT approval.*,
(select diag.*
from diag diag 
where approval.id =diag.id
FOR XML PATH('diag'), TYPE
),
(select ser.*
from ser ser
where approval.id =ser.id
FOR XML PATH('ser'), TYPE
)
FROM approval approval,
where approval.id = 1
FOR XML PATH, ELEMENTS,
root('Head')