我有以下 XML
<Queue>
<UserName>UserName</UserName>
<Type>1</Type>
<Portal name="Portal1">
<IndexesForSelect>
<Index>Index1</Index>
<Index>Index2</Index>
</IndexesForSelect>
</Portal>
<Portal name="Portal2">
<IndexesForSelect>
<Index>Index3</Index>
<Index>Index4</Index>
</IndexesForSelect>
</Portal>
</Queue>
我需要把它放到下面格式的表格中
Portal Index
---------------------------
Portal1 Index1
Portal1 Index2
Portal2 Index3
Portal2 Index4
如果有人可以帮助我,我真的很感激。
我已经尝试了以下代码,但它只返回第Index
一个Portal
declare @T table
(
XMLCol xml
)
insert into @T values
('<Queue>
<UserName>UserName</UserName>
<Type>1</Type>
<Portal name="Portal1">
<IndexesForSelect>
<Index>Index1</Index>
<Index>Index2</Index>
</IndexesForSelect>
</Portal>
<Portal name="Portal2">
<IndexesForSelect>
<Index>Index3</Index>
<Index>Index4</Index>
</IndexesForSelect>
</Portal>
</Queue>')
SELECT items.value('../@name','varchar(max)') AS [Portal],
items.value('(Index)[1]','varchar(max)') AS [Index]
FROM @T AS T CROSS APPLY T.XMLCol.nodes('Queue/Portal/IndexesForSelect') c (items)