我有以下 XML:
<root>
<row value="US">
<col value="00">Jon</col>
<col value="01">David</col>
<col value="02">Mike</col>
<col value="03">Nil</col>
</row>
<row value="Canada">
<col value="C1">Pollard</col>
</row>
<row value="Japan">
<col value="J1">Yin</col>
<col value="J2">Li</col>
</row>
<row value="India">
<col value="MP">Ram</col>
<col value="UP">Paresh</col>
<col value="AP">Mohan</col>
</row>
</root>
我想要通过使用 SQL Server 查询得到以下输出:
US 00 Jon
US 01 David
US 02 Mike
US 03 Nil
Canada C1 Pollard
Japan J1 Yin
Japan J2 Li
India MP Ram
India UP Paresh
India AP Mohan
我正在使用以下 SQL 查询:
declare @x xml
set @x =
'<root>
<row value="US">
<col value="00">Jon</col>
<col value="01">David</col>
<col value="02">Mike</col>
<col value="03">Nil</col>
</row>
<row value="Canada">
<col value="C1">Pollard</col>
</row>
<row value="Japan">
<col value="J1">Yin</col>
<col value="J2">Li</col>
</row>
<row value="India">
<col value="MP">Ram</col>
<col value="UP">Paresh</col>
<col value="AP">Mohan</col>
</row>
</root>'
select r.value('@value','varchar(100)'),r.value('.','varchar(100)')
from @x.nodes('root') as m(c)
cross apply m.c.nodes('row/col') as x(r)
我无法获得包含父行值的第一列。您能否建议我可以进行哪些更改以获得第一列值?