我正在尝试通过 TSQL 在 sql server 2008 中解析/分解 XML,其中一个节点具有以下格式的空白属性,没有指定值。我似乎无法检索该值,并且它抛弃了所有其他值(移动列?)
这是 tsql 代码和错误的图片:
declare @doc nvarchar(4000)
set @doc = '
<Objects>
<Object>
<Property Name="Path">some path</Property>
<Property Name="InstanceName">some instance</Property>
<Property Name="result1">0.390630000000003</Property>
<Property Name="result2">63345649697265</Property>
</Object>
<Object>
<Property Name="Path">another path</Property>
<Property Name="InstanceName" />
<Property Name="result1">100</Property>
<Property Name="result2">1002</Property>
</Object>
</Objects>
'
SELECT
item.ref.value('(Property/text())[1]', 'nvarchar(128)') AS Path,
item.ref.value('(Property/text())[2]', 'nvarchar(128)') AS InstanceName,
item.ref.value('(Property/text())[3]', 'nvarchar(128)') AS result1,
item.ref.value('(Property/text())[4]', 'nvarchar(128)') AS result2
FROM (SELECT CAST(@doc AS XML) AS feedXml) feeds(feedXml)
CROSS APPLY feedXml.nodes('/Objects/Object') AS item(ref)
当您运行查询时,请注意 InstanceName 列填充了另一列中的值,而不是空白、空或 null。
任何帮助表示赞赏。