sqlsql-serverxmlt-sqlxquery-sql

Select all XML nodes and generate SQL Server table


I got stuck in SQL - can anyone please help?

I have a XML shown below; I want to read the XML tags and create a table in SQL Server.

I tried the query also shown below, but that did not fetch the data. The result set is empty. I have used cross apply and cast to xml.

Apparently I can write XPath queries to select tags into columns, which is what I want, except there is like 1000 possible tag names, and I don't want to write them all out (and possibly miss one) and hence i am looking for solution that we read xml tags and generate table with values and tags in SQL.

SELECT 
    t.ID,
    T0.Color.value('local-name(.)', 'nvarchar(max)') AS TagName,
    T0.Color.value('(@name)[1]','nvarchar(max)') AS EntityName,
    T0.Color.value('text()[1]','nvarchar(max)') AS TagValue  
FROM
    ConfigurationDnaItem t
CROSS APPLY 
    (SELECT 
         CAST(t.Value AS XML)) AS A(AllNodes)
CROSS APPLY 
    A.AllNodes.nodes('/hierarchy/nodes/list-entity') AS T0(Color)
WHERE 
    id = '1D09BFCB-AE0A-4346-835D-4BBCF2BBB503'

Output Required

Please see the above table structure I am looking for remove from selection N/A the last row that would be much better.

Thanks for help.

<?xml version="1.0" encoding="utf-16"?>
<hierarchy name="DGPaintH" xmlns="urn:guru.cincom.com-Hierarchy">
    <nodes>
        <list-entity name="HubPaintDetail">
          <values>
            <value>
              <key-value><![CDATA[AFL.9H1B]]></key-value>
              <characteristics>
                <characteristic name="Value" type="list"><![CDATA[AFL.9H1B]]></characteristic>
                <characteristic name="Description" type="string"><![CDATA[Fleet Red(PPG Code: AFL.9H1B)]]></characteristic>
                <characteristic name="JDE_ITEM_NUMBER" type="string"><![CDATA[]]></characteristic>
                <characteristic name="PRICE_EX_FACTORY" type="numeric">0.00</characteristic>
                <characteristic name="PRICE_RETAIL" type="numeric">0.00</characteristic>
                <characteristic name="PRICE_DIFFERENTIAL" type="numeric">0.00</characteristic>
                <characteristic name="OPTION_WEIGHT" type="numeric">0.00</characteristic>
                <characteristic name="WEIGHT_DIFFERENTIAL" type="numeric">0.00</characteristic>
                <characteristic name="QUANTITY" type="numeric">0</characteristic>
                <characteristic name="Name" type="string"><![CDATA[]]></characteristic>
                <characteristic name="CatCode1" type="string"><![CDATA[02]]></characteristic>
                <characteristic name="CatCode2" type="string"><![CDATA[33]]></characteristic>
                <characteristic name="CatCode3" type="string"><![CDATA[DP]]></characteristic>
              </characteristics>
            </value>
          </values>
          <extended-properties>
            <extended-property name="ALTNAME" type="string"><![CDATA[Hubs]]></extended-property>
            <extended-property name="CatCode1" type="string"><![CDATA[02]]></extended-property>
            <extended-property name="CatCode2" type="string"><![CDATA[33]]></extended-property>
            <extended-property name="CatCode3" type="string"><![CDATA[DP]]></extended-property>
          </extended-properties>
        </list-entity>
        <list-entity name="DoorPaintDetail">
          <values>
            <value>
              <key-value><![CDATA[AFL.DTT6]]></key-value>
              <characteristics>
                <characteristic name="Value" type="list"><![CDATA[AFL.DTT6]]></characteristic>
                <characteristic name="Description" type="string"><![CDATA[GELCOAT WHITE]]></characteristic>
                <characteristic name="JDE_ITEM_NUMBER" type="string"><![CDATA[]]></characteristic>
                <characteristic name="PRICE_EX_FACTORY" type="numeric">0.00</characteristic>
                <characteristic name="PRICE_RETAIL" type="numeric">0.00</characteristic>
                <characteristic name="PRICE_DIFFERENTIAL" type="numeric">0.00</characteristic>
                <characteristic name="OPTION_WEIGHT" type="numeric">0.00</characteristic>
                <characteristic name="WEIGHT_DIFFERENTIAL" type="numeric">0.00</characteristic>
                <characteristic name="QUANTITY" type="numeric">0</characteristic>
                <characteristic name="Name" type="string"><![CDATA[]]></characteristic>
                <characteristic name="CatCode1" type="string"><![CDATA[02]]></characteristic>
                <characteristic name="CatCode2" type="string"><![CDATA[33]]></characteristic>
                <characteristic name="CatCode3" type="string"><![CDATA[DS]]></characteristic>
              </characteristics>
            </value>
          </values>
          <extended-properties>
            <extended-property name="ALTNAME" type="string"><![CDATA[Door]]></extended-property>
            <extended-property name="CatCode1" type="string"><![CDATA[02]]></extended-property>
            <extended-property name="CatCode2" type="string"><![CDATA[33]]></extended-property>
            <extended-property name="CatCode3" type="string"><![CDATA[DS]]></extended-property>
          </extended-properties>
        </list-entity>
        <list-entity name="UnderPaintDetail">
          <values>
            <value>
              <key-value><![CDATA[AFL.9H1B]]></key-value>
              <characteristics>
                <characteristic name="Value" type="list"><![CDATA[AFL.9H1B]]></characteristic>
                <characteristic name="Description" type="string"><![CDATA[Fleet Red(PPG Code: AFL.9H1B)]]></characteristic>
                <characteristic name="JDE_ITEM_NUMBER" type="string"><![CDATA[]]></characteristic>
                <characteristic name="PRICE_EX_FACTORY" type="numeric">0.00</characteristic>
                <characteristic name="PRICE_RETAIL" type="numeric">0.00</characteristic>
                <characteristic name="PRICE_DIFFERENTIAL" type="numeric">0.00</characteristic>
                <characteristic name="OPTION_WEIGHT" type="numeric">0.00</characteristic>
                <characteristic name="WEIGHT_DIFFERENTIAL" type="numeric">0.00</characteristic>
                <characteristic name="QUANTITY" type="numeric">0</characteristic>
                <characteristic name="Name" type="string"><![CDATA[]]></characteristic>
                <characteristic name="CatCode1" type="string"><![CDATA[02]]></characteristic>
                <characteristic name="CatCode2" type="string"><![CDATA[33]]></characteristic>
                <characteristic name="CatCode3" type="string"><![CDATA[DM]]></characteristic>
              </characteristics>
            </value>
          </values>
          <extended-properties>
            <extended-property name="ALTNAME" type="string"><![CDATA[Under]]></extended-property>
            <extended-property name="CatCode1" type="string"><![CDATA[02]]></extended-property>
            <extended-property name="CatCode2" type="string"><![CDATA[33]]></extended-property>
            <extended-property name="CatCode3" type="string"><![CDATA[DM]]></extended-property>
          </extended-properties>
        </list-entity>
        <list-entity name="PaintFleetMasterBoxesList">
          <values>
            <value>
              <key-value><![CDATA[AFL.9H1B]]></key-value>
              <characteristics>
                <characteristic name="Value" type="list"><![CDATA[AFL.9H1B]]></characteristic>
                <characteristic name="Description" type="string"><![CDATA[Fleet Red(PPG Code: AFL.9H1B)]]></characteristic>
                <characteristic name="JDE_ITEM_NUMBER" type="string"><![CDATA[]]></characteristic>
                <characteristic name="PRICE_EX_FACTORY" type="numeric">0.00</characteristic>
                <characteristic name="PRICE_RETAIL" type="numeric">0.00</characteristic>
                <characteristic name="PRICE_DIFFERENTIAL" type="numeric">0.00</characteristic>
                <characteristic name="OPTION_WEIGHT" type="numeric">0.00</characteristic>
                <characteristic name="WEIGHT_DIFFERENTIAL" type="numeric">0.00</characteristic>
                <characteristic name="QUANTITY" type="numeric">0</characteristic>
                <characteristic name="HasNSO" type="boolean">false</characteristic>
                <characteristic name="CatCode1" type="string"><![CDATA[02]]></characteristic>
                <characteristic name="CatCode2" type="string"><![CDATA[33]]></characteristic>
                <characteristic name="CatCode3" type="string"><![CDATA[DS]]></characteristic>
              </characteristics>
            </value>
          </values>
          <extended-properties>
            <extended-property name="ALTNAME" type="string"><![CDATA[Boxes]]></extended-property>
          </extended-properties>
        </list-entity>
        <list-entity name="RadiusGuardPaint">
          <values>
            <value>
              <key-value><![CDATA[N/A]]></key-value>
              <characteristics>
                <characteristic name="Value" type="list"><![CDATA[N/A]]></characteristic>
                <characteristic name="Description" type="string"><![CDATA[Guard Colour not applicable]]></characteristic>
                <characteristic name="JDE_ITEM_NUMBER" type="string"><![CDATA[]]></characteristic>
                <characteristic name="PRICE_EX_FACTORY" type="numeric">0.00</characteristic>
                <characteristic name="PRICE_RETAIL" type="numeric">0.00</characteristic>
                <characteristic name="PRICE_DIFFERENTIAL" type="numeric">0.00</characteristic>
                <characteristic name="OPTION_WEIGHT" type="numeric">0.00</characteristic>
                <characteristic name="WEIGHT_DIFFERENTIAL" type="numeric">0.00</characteristic>
                <characteristic name="QUANTITY" type="numeric">1</characteristic>
              </characteristics>
            </value>
          </values>
          <extended-properties>
            <extended-property name="ALTNAME" type="string"><![CDATA[Radius Guard Colour]]></extended-property>
            <extended-property name="CatCode1" type="string"><![CDATA[02]]></extended-property>
            <extended-property name="CatCode2" type="string"><![CDATA[33]]></extended-property>
            <extended-property name="CatCode3" type="string"><![CDATA[DS]]></extended-property>
          </extended-properties>
        </list-entity>
    </nodes>
</hierarchy>

Solution

  • It's rather unclear what your expected output is, but it looks like you want something like this

    WITH XMLNAMESPACES(DEFAULT 'urn:guru.cincom.com-Hierarchy')
    SELECT t.ID
          ,T0.Color.value('(extended-properties/extended-property[@name = "ALTNAME"]/text())[1]', 'nvarchar(max)') AS TagName
          ,T0.Color.value('@name','nvarchar(max)') AS EntityName
          ,T0.Color.value('(values/value/key-value/text())[1]','nvarchar(max)') AS TagValue  
    FROM ConfigurationDnaItem t
    CROSS APPLY (SELECT CAST(t.Value AS XML)) As A(AllNodes)
    CROSS APPLY A.AllNodes.nodes('/hierarchy/nodes/list-entity[not(values/value/key-value/text() = "N/A")]') AS T0(Color)
    where t.id = '1D09BFCB-AE0A-4346-835D-4BBCF2BBB503';
    

    There were a number of issues with your existing query:

    db<>fiddle