1

我有以下 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)
4

2 回答 2

1
select T1.N.value('@name', 'varchar(max)') as Portal,
       T2.N.value('.', 'varchar(max)') as [Indes]
from @T as T
  cross apply XMLCol.nodes('/Queue/Portal') as T1(N)
  cross apply T1.N.nodes('IndexesForSelect/Index') as T2(N)
于 2012-07-27T06:34:01.197 回答
0

尝试使用 SQL 管理工作室提供的导出/导入功能。

阅读它去这里

于 2012-07-27T06:32:57.403 回答