Select 所有 XML 节点并生成 SQL 服务器 table
Select all XML nodes and generate SQL Server table
我卡在了 SQL - 谁能帮忙吗?
我有一个 XML 如下所示;我想读取 XML 标签并在 SQL 服务器中创建一个 table。
我尝试了如下所示的查询,但没有获取数据。结果集为空。我已经使用交叉应用并转换为 xml.
显然我可以将对 select 标签的 XPath 查询写入列中,这就是我想要的,除了有大约 1000 个可能的标签名称,我不想把它们全部写出来(并且可能错过了一个),因此我正在寻找我们读取 xml 标签并生成 table 的解决方案,其中的值和标签位于 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
请看上面的 table 结构我要从 selection N/A 中删除最后一行会好得多。
感谢您的帮助。
<?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>
不太清楚您的预期输出是什么,但看起来您想要这样的结果
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';
您现有的查询存在一些问题:
- 您缺少命名空间声明。
local-name(.)
会为您提供节点名称 list-entity
,目前尚不清楚您为什么想要它。
- 你似乎想要的一些数据存储在
extended-properties/extended-property
中,你需要通过属性name
过滤,然后拉出内部文本。
@name
为您提供 name
属性,该属性不存在于您的图像中。但是我已经把它留在里面了。它不需要 [1]
.
text()[1]
不会给你任何东西,因为上下文节点没有任何直接的内部文本节点。相反,您似乎想下降到 values/value/key-value/text()
.
- 要过滤掉
N/A
值,需要如下谓词[not(values/value/key-value/text() = "N/A")]
我卡在了 SQL - 谁能帮忙吗?
我有一个 XML 如下所示;我想读取 XML 标签并在 SQL 服务器中创建一个 table。
我尝试了如下所示的查询,但没有获取数据。结果集为空。我已经使用交叉应用并转换为 xml.
显然我可以将对 select 标签的 XPath 查询写入列中,这就是我想要的,除了有大约 1000 个可能的标签名称,我不想把它们全部写出来(并且可能错过了一个),因此我正在寻找我们读取 xml 标签并生成 table 的解决方案,其中的值和标签位于 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
请看上面的 table 结构我要从 selection N/A 中删除最后一行会好得多。
感谢您的帮助。
<?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>
不太清楚您的预期输出是什么,但看起来您想要这样的结果
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';
您现有的查询存在一些问题:
- 您缺少命名空间声明。
local-name(.)
会为您提供节点名称list-entity
,目前尚不清楚您为什么想要它。- 你似乎想要的一些数据存储在
extended-properties/extended-property
中,你需要通过属性name
过滤,然后拉出内部文本。 @name
为您提供name
属性,该属性不存在于您的图像中。但是我已经把它留在里面了。它不需要[1]
.text()[1]
不会给你任何东西,因为上下文节点没有任何直接的内部文本节点。相反,您似乎想下降到values/value/key-value/text()
.- 要过滤掉
N/A
值,需要如下谓词[not(values/value/key-value/text() = "N/A")]