将SQL中的XML字符串解析为特定列

问题描述 投票:0回答:1

我的XML文件如下所示:

<CIM CIMVERSION="2.0" DTDVERSION="2.0">
  <MESSAGE ID="4711" PROTOCOLVERSION="1.0">
    <SIMPLEREQ>
      <VALUE.NAMEDINSTANCE>
        <INSTANCENAME CLASSNAME="DCIM_ControllerView">
          <KEYBINDING NAME="InstanceID">
            <KEYVALUE VALUETYPE="string">RAID.Integrated.1-1</KEYVALUE>
          </KEYBINDING>
        </INSTANCENAME>
        <INSTANCE CLASSNAME="DCIM_ControllerView">
          <PROPERTY NAME="LastUpdateTime" TYPE="string">
            <VALUE>20170223172914.000000+000</VALUE>
            <DisplayValue>2017-02-23T17:29:14</DisplayValue>
          </PROPERTY>
          <PROPERTY NAME="LastSystemInventoryTime" TYPE="string">
            <VALUE>20170223172914.000000+000</VALUE>
            <DisplayValue>2017-02-23T17:29:14</DisplayValue>
          </PROPERTY>
          <PROPERTY NAME="RealtimeCapability" TYPE="uint32">
            <VALUE>1</VALUE>
            <DisplayValue>Capable</DisplayValue>
          </PROPERTY>
        </INSTANCE>
      </VALUE.NAMEDINSTANCE>
      <VALUE.NAMEDINSTANCE>
        <INSTANCENAME CLASSNAME="DCIM_ControllerView">
          <KEYBINDING NAME="InstanceID">
            <KEYVALUE VALUETYPE="string">AHCI.Embedded.1-1</KEYVALUE>
          </KEYBINDING>
        </INSTANCENAME>
        <INSTANCE CLASSNAME="DCIM_ControllerView">
          <PROPERTY NAME="LastUpdateTime" TYPE="string">
            <VALUE>20170106154535.000000+000</VALUE>
            <DisplayValue>2017-01-06T15:45:35</DisplayValue>
          </PROPERTY>
          <PROPERTY NAME="LastSystemInventoryTime" TYPE="string">
            <VALUE>20170223170150.000000+000</VALUE>
            <DisplayValue>2017-02-23T17:01:50</DisplayValue>
          </PROPERTY>
          <PROPERTY NAME="RealtimeCapability" TYPE="uint32">
            <VALUE>0</VALUE>
            <DisplayValue>Incapable</DisplayValue>
          </PROPERTY>         
        </INSTANCE>
      </VALUE.NAMEDINSTANCE>
      <VALUE.NAMEDINSTANCE>
        <INSTANCENAME CLASSNAME="DCIM_ControllerView">
          <KEYBINDING NAME="InstanceID">
            <KEYVALUE VALUETYPE="string">AHCI.Embedded.2-1</KEYVALUE>
          </KEYBINDING>
        </INSTANCENAME>
        <INSTANCE CLASSNAME="DCIM_ControllerView">
          <PROPERTY NAME="LastUpdateTime" TYPE="string">
            <VALUE>20170106154535.000000+000</VALUE>
            <DisplayValue>2017-01-06T15:45:35</DisplayValue>
          </PROPERTY>
          <PROPERTY NAME="LastSystemInventoryTime" TYPE="string">
            <VALUE>20170223170150.000000+000</VALUE>
            <DisplayValue>2017-02-23T17:01:50</DisplayValue>
          </PROPERTY>
          <PROPERTY NAME="RealtimeCapability" TYPE="uint32">
            <VALUE>0</VALUE>
            <DisplayValue>Incapable</DisplayValue>
          </PROPERTY>         
        </INSTANCE>
      </VALUE.NAMEDINSTANCE>
      <VALUE.NAMEDINSTANCE>
        <INSTANCENAME CLASSNAME="DCIM_MemoryView">
          <KEYBINDING NAME="InstanceID">
            <KEYVALUE VALUETYPE="string">DIMM.Socket.A1</KEYVALUE>
          </KEYBINDING>
        </INSTANCENAME>
        <INSTANCE CLASSNAME="DCIM_MemoryView">
          <PROPERTY NAME="LastUpdateTime" TYPE="string">
            <VALUE>20170106144535.000000+000</VALUE>
            <DisplayValue>2017-01-06T14:45:35</DisplayValue>
          </PROPERTY>        
        </INSTANCE>
      </VALUE.NAMEDINSTANCE>
    </SIMPLEREQ>
  </MESSAGE>
</CIM>

使用SQL,我想将我的数据放入这些特定的列:

CIM | Messgae | Classname | InstanceID | PropertyName |价值| DisplayValue

我很抱歉,因为这个问题非常类似于此处发布的问题:Parse XML file using SQL into Specific Columns

但是我被用户批评再次发布问题,因为我稍微改变了我的XML文档。

帮助演示XML模式的图表:

Diagram

sql-server xml parsing
1个回答
1
投票

试试这样:

SELECT @xml.value(N'(/CIM/@CIMVERSION)[1]',N'nvarchar(max)') AS CIM_version
      ,@xml.value(N'(/CIM/MESSAGE/@ID)[1]',N'nvarchar(max)') AS Message_Id

      ,vni.value(N'(INSTANCENAME/KEYBINDING/KEYVALUE/text())[1]',N'nvarchar(max)') AS Keybinding_Value

      ,vni.value(N'(INSTANCE/@CLASSNAME)[1]',N'nvarchar(max)') AS Instance_ClassName

      ,prp.value(N'@NAME',N'nvarchar(max)') AS Prop_Name
      ,prp.value(N'(VALUE/text())[1]',N'nvarchar(max)') AS Prop_Value
      ,prp.value(N'(DisplayValue/text())[1]',N'nvarchar(max)') AS Prop_DisplayValue
FROM @xml.nodes(N'/CIM/MESSAGE/SIMPLEREQ/VALUE.NAMEDINSTANCE') AS A(vni)
CROSS APPLY vni.nodes('INSTANCE/PROPERTY') AS B(prp);

某些值在xml中只有一次。我直接从你的@xml取他们。作为第二级,你有重复元素<VALUE.NAMEDINSTANCE>和 - 嵌入 - 重复元素<PROPERTY>

结果

+-------------+------------+---------------------+---------------------+-------------------------+---------------------------+---------------------+
| CIM_version | Message_Id | Keybinding_Value    | Instance_ClassName  | Prop_Name               | Prop_Value                | Prop_DisplayValue   |
+-------------+------------+---------------------+---------------------+-------------------------+---------------------------+---------------------+
| 2.0         | 4711       | RAID.Integrated.1-1 | DCIM_ControllerView | LastUpdateTime          | 20170223172914.000000+000 | 2017-02-23T17:29:14 |
+-------------+------------+---------------------+---------------------+-------------------------+---------------------------+---------------------+
| 2.0         | 4711       | RAID.Integrated.1-1 | DCIM_ControllerView | LastSystemInventoryTime | 20170223172914.000000+000 | 2017-02-23T17:29:14 |
+-------------+------------+---------------------+---------------------+-------------------------+---------------------------+---------------------+
| 2.0         | 4711       | RAID.Integrated.1-1 | DCIM_ControllerView | RealtimeCapability      | 1                         | Capable             |
+-------------+------------+---------------------+---------------------+-------------------------+---------------------------+---------------------+
| 2.0         | 4711       | AHCI.Embedded.1-1   | DCIM_ControllerView | LastUpdateTime          | 20170106154535.000000+000 | 2017-01-06T15:45:35 |
+-------------+------------+---------------------+---------------------+-------------------------+---------------------------+---------------------+
| 2.0         | 4711       | AHCI.Embedded.1-1   | DCIM_ControllerView | LastSystemInventoryTime | 20170223170150.000000+000 | 2017-02-23T17:01:50 |
+-------------+------------+---------------------+---------------------+-------------------------+---------------------------+---------------------+
| 2.0         | 4711       | AHCI.Embedded.1-1   | DCIM_ControllerView | RealtimeCapability      | 0                         | Incapable           |
+-------------+------------+---------------------+---------------------+-------------------------+---------------------------+---------------------+
| 2.0         | 4711       | AHCI.Embedded.2-1   | DCIM_ControllerView | LastUpdateTime          | 20170106154535.000000+000 | 2017-01-06T15:45:35 |
+-------------+------------+---------------------+---------------------+-------------------------+---------------------------+---------------------+
| 2.0         | 4711       | AHCI.Embedded.2-1   | DCIM_ControllerView | LastSystemInventoryTime | 20170223170150.000000+000 | 2017-02-23T17:01:50 |
+-------------+------------+---------------------+---------------------+-------------------------+---------------------------+---------------------+
| 2.0         | 4711       | AHCI.Embedded.2-1   | DCIM_ControllerView | RealtimeCapability      | 0                         | Incapable           |
+-------------+------------+---------------------+---------------------+-------------------------+---------------------------+---------------------+
| 2.0         | 4711       | DIMM.Socket.A1      | DCIM_MemoryView     | LastUpdateTime          | 20170106144535.000000+000 | 2017-01-06T14:45:35 |
+-------------+------------+---------------------+---------------------+-------------------------+---------------------------+---------------------+
© www.soinside.com 2019 - 2024. All rights reserved.