如何以下面提到的格式阅读此XML - SQL Server 2012

时间:2015-03-06 09:16:15

标签: sql-server xml sql-server-2012

任何帮助将不胜感激!尝试将以下XML转换为SQL Server中的表结构。

<SearchKey EntityType="2" userid="12" QName="search1">
    <Groups>
        <Group seq="1">
            <GroupID>1</GroupID>
            <Column seq="1">2</Column>
            <Column seq="2">3</Column>
        </Group>
        <Group seq="2">
            <GroupID>2</GroupID>
            <Column seq="1">1</Column>
            <Column seq="2">2</Column>
        </Group>
        <Group seq="3">
            <GroupID>3</GroupID>
            <Group seq="1">1</Group>
            <Column seq="2">2</Column>
        </Group>
        <Group seq="4">
            <GroupID>4</GroupID>
            <Group seq="1">1</Group>
            <Group seq="2">2</Group>
            <Column seq="3">3</Column>
        </Group>
    </Groups>
</SearchKey>

尝试使用以下代码读取XML,但未达到预期的输出。请帮忙!

SELECT a.b.value('GroupID[1]', 'varchar(10)') AS GroupID
    ,a.b.value('Column[1]', 'varchar(10)') AS ColumnSeq
    ,a.b.value('GroupID[1]', 'varchar(10)') AS GroupID1
FROM @XMLString.nodes('/Groups/Group') a(b)

在下表格式中:

GroupID SeqID   ColumnValue ChildGroupID
1       1       2           NULL
1       2       3           NULL
3       1       NULL        1
3       2       2           NULL
4       1       NULL        1
4       2       NULL        2
4       3       3           NULL

还尝试了以下代码,但无法以上述格式检索记录:

SELECT a.b.value('GroupID[1]', 'varchar(10)') AS GroupID
    ,a.b.value('(@GrpSeq)[1]', 'varchar(50)')  AS GrpSeq
    ,a.b.value('(GroupID)[1]', 'varchar(50)')  AS GroupID
    ,a.b.value('(Column/@ColSeq)[1]', 'varchar(50)') AS ColSeq
FROM @XMLString.nodes('SearchKey/Groups/Group') a(b)

感谢所有的帮助:)

0 个答案:

没有答案