我使用的是Microsoft SQL Server 2008 R2并且不熟悉XML。我有一个查询生成如下所示的输出:
<user id="12345"> --SAME USER ID
<user_type>STUDENT</user_type>
<name_first>Jane</name_first>
<name_last>Smith</name_last>
<email>jsmith@somedomain.com</email>
<transcript>
<course id="ACC101">
<title>Accounting 101</title>
<credits>3.0000</credits>
<grade>B</grade>
</course>
</transcript>
</user>
<user id="12345"> --SAME USER ID
<user_type>STUDENT</user_type>
<name_first>Jane</name_first>
<name_last>Smith</name_last>
<email>jsmith@somedomain.com</email>
<transcript>
<course id="ENG151">
<title>English 151</title>
<credits>3.0000</credits>
<grade>A</grade>
</course>
</transcript>
</user>
<user id="12345"> --SAME USER ID
<user_type>STUDENT</user_type>
<name_first>Jane</name_first>
<name_last>Smith</name_last>
<email>jsmith@somedomain.com</email>
<transcript>
<course id="MAT102">
<title>Math 102</title>
<credits>3.0000</credits>
<grade>B</grade>
</course>
</transcript>
</user>
但我希望它看起来像这样:
<user id="12345"> -- ALL DATA SHOULD BE WITHIN THE USER TAG
<user_type>STUDENT</user_type>
<name_first>Jane</name_first>
<name_last>Smith</name_last>
<email>jsmith@somedomain.com</email>
<transcript> -- ALL COURSE DATA SHOULD BE WITHIN THE TRANSCRIPT TAG
<course id="ABC123">
<title>Accounting 101</title>
<credits>3.0000</credits>
<grade>B</grade>
</course>
<course id="ENG151">
<title>English 151</title>
<credits>3.0000</credits>
<grade>A</grade>
</course>
<course id="MAT102">
<title>Math 102</title>
<credits>3.0000</credits>
<grade>B</grade>
</course>
</transcript>
</user>
如果有更多可以提供的信息,请告诉我。提前感谢您的帮助!
答案 0 :(得分:0)
SELECT TOP 5 User_Type, name_first, name_last
FROM Users
FOR XML AUTO, ROOT('User'), ELEMENTS
将用户添加到ROOT
会将所有其他元素放在其下方。希望这有帮助!