xml节点值及其在sql中的相应父节点

时间:2013-10-03 09:17:38

标签: sql sql-server sql-server-2008-r2 sqlxml

<ExecutionSequence>
    <property name="Id">1437</property>
    <property name="Name">seq</property>
        <ExecutionBlock>
            <property name="Name">Block1</property>
            <ExecutionCommand>
                <property name="Name">Cmd1</property>
            </ExecutionCommand>
            <ExecutionCommand>
                <property name="Name">Cmd2</property>
            </ExecutionCommand>
        </ExecutionBlock>
    <ExecutionBlock>
        <property name="Name">Block2</property>
        <ExecutionCommand>
            <property name="Name">Cmd1</property>
        </ExecutionCommand>
        <ExecutionCommand>
            <property name="Name">Cmd2</property>
        </ExecutionCommand>
    </ExecutionBlock>
</ExecutionSequence>

我希望所有的商品名称都带有各自的父名称

seq Block1 cmd1

seq Block1 cmd2

seq Block2 cmd1

seq Block2 cmd2

1 个答案:

答案 0 :(得分:0)

select
    e.c.value('(property[@name="Name"]/text())[1]', 'nvarchar(max)') as Sequence,
    b.c.value('(property[@name="Name"]/text())[1]', 'nvarchar(max)') as Block,
    c.c.value('(property[@name="Name"]/text())[1]', 'nvarchar(max)') as Command
from @data.nodes('ExecutionSequence') as e(c)
    outer apply e.c.nodes('ExecutionBlock') as b(c)
    outer apply b.c.nodes('ExecutionCommand') as c(c)

<强> sql fiddle demo