我在SQL XML中有以下代码。我需要在SQL XML中将父元素插入父元素。
我有下表。
Firstname Lastname class mark
John Lim V 60
Wills smith V 80
Mark Bay V 75
Carl mark V 65
这是我的SQL XML语句
Select
Firstname,
Lastname,
class,
mark
from Student for xml path('Students'), type
结果如下
<Students>
<Firstname>John</Firstname>
<Lastname>Lim</Lastname>
<class>V</class>
<mark>60</mark>
</Students>
<Students>
<Firstname>Wills</Firstname>
<Lastname>smith</Lastname>
<class>V</class>
<mark>80</mark>
</Students>
<Students>
<Firstname>Mark</Firstname>
<Lastname>Bay</Lastname>
<class>V</class>
<mark>75</mark>
</Students>
<Students>
<Firstname>Carl</Firstname>
<Lastname>mark</Lastname>
<class>V</class>
<mark>65</mark>
</Students>
我希望下面的结果是class和mark标签包含在另一个标签中:
<Students>
<Firstname>John</Firstname>
<Lastname>Lim</Lastname>
<Details>
<class>V</class>
<mark>60</mark>
</Details>
</Students>
<Students>
<Firstname>Wills</Firstname>
<Lastname>smith</Lastname>
<Details>
<class>V</class>
<mark>80</mark>
</Details>
</Students>
<Students>
<Firstname>Mark</Firstname>
<Lastname>Bay</Lastname>
<Details>
<class>V</class>
<mark>75</mark>
</Details>
</Students>
<Students>
<Firstname>Carl</Firstname>
<Lastname>mark</Lastname>
<Details>
<class>V</class>
<mark>65</mark>
</Details>
</Students>
知道怎么做这个吗?
谢谢!
答案 0 :(得分:2)
您可以使用列别名。
select Firstname,
Lastname,
class as "Details/class",
mark as "Details/mark"
from Student
for xml path('Students'), type