我有以下格式的数据:
user Id Value
-------------------------------
a 50000 5
a 50000 6
a 50000 7
b 50001 8
b 50001 9
b 50001 10
我想获得以下格式的格式化 XML
<RootNode>
<User "a">
<UserAttribute Id="50000" value="5"/>
<UserAttribute Id="50000" value="6"/>
<UserAttribute Id="50000" value="7"/>
</User>
<User "b">
<UserAttribute Id="50001" value="8"/>
<UserAttribute Id="50001" value="9"/>
<UserAttribute Id="50001" value="10"/>
</User>
</RootNode>
我尝试了以下选择查询:
select
[user] as '@user',
[id] as 'UserAttribute/@id',
[value] as 'UserAttribute/@value'
from
dbo.test
for xml path('User')
但它没有提供所需的输出。有人可以让我知道我错过了什么吗?
当前输出:
<User user="a">
<UserAttribute id="50000" value="5" />
</User>
<User user="a">
<UserAttribute id="50000" value="6" />
</User>
<User user="a">
<UserAttribute id="50000" value="7" />
</User>
<User user="b">
<UserAttribute id="50001" value="8" />
</User>
<User user="b">
<UserAttribute id="50001" value="8" />
</User>
<User user="b">
<UserAttribute id="50001" value="8" />
</User>