如何在SQL Server 2008中解析xml

时间:2018-05-05 18:14:12

标签: sql sql-server sql-server-2008

我有以下XML:

<PW_main_Root_>
    <Trigger_data_new_ID>30</Trigger_data_new_ID>
    <Trigger_Event>
        <OperatorID>30</OperatorID>
        <MainData>516274981</MainData>
        <Variables>
            <Variable VarID="8040" Value="00000000"/>
            <Variable VarID="8140" Value="50000000"/>
            <Variable VarID="8240" Value="00000000"/>
            <Variable VarID="8340" Value="00000000"/>
            <Variable VarID="8440" Value="00000000"/>
            <Variable VarID="8540" Value="00000000"/>
            <Variable VarID="8640" Value="00080000"/>
            <Variable VarID="8740" Value="01000000"/>
            <Variable VarID="8840" Value="000E0000"/>
            <Variable VarID="8940" Value="00000000"/>
            <Variable VarID="8B40" Value="0F000000"/>
            <Variable VarID="8A40" Value="00000000"/>
            <Variable VarID="0F40" Value="00000000"/>
        </Variables>
    </Trigger_Event>
</PW_main_Root_>

现在我想在sql server中解析这个XML,并希望它像:

VarID    Value
-------------------
8040    00000000
8140    50000000
8240    00000000
...and so on

我该怎么做?

我有SQL Server 2008,因此SQL Server 2014的所有功能都不适用于此。

1 个答案:

答案 0 :(得分:2)

示例

Select VarID = x.v.value('@VarID','VARCHAR(50)')
      ,Value = x.v.value('@Value','VARCHAR(50)')
 From  @Xml.nodes('PW_main_Root_/Trigger_Event/Variables/Variable') x(v)

<强>返回

VarID   Value
8040    00000000
8140    50000000
8240    00000000
8340    00000000
8440    00000000
8540    00000000
8640    00080000
8740    01000000
8840    000E0000
8940    00000000
8B40    0F000000
8A40    00000000
0F40    00000000