您好我正在尝试从包含XML的列中提取数据。我试过搜索,但似乎无法做到正确。列名称为“设置”,此处是列中某个xml中的一些。我试图获取列中所有xmls的MacAddress条目的值。该表名为Hardware.Thanks以获得任何帮助。
我尝试过这个返回所有值的查询。
select uri, settings.value('(/properties)[1]', 'varchar(max)') as
MacAddress From Hardware Go
<properties>
<settings hwid="hardware">
<setting typeid="928B6039-4E4A-4B03-9A82-A1CF29748ACC">
<name>DetectedModelName</name>
<value>AXIS P3365 Fixed Dome Network Camera</value>
</setting>
<setting typeid="DAF813F8-2E02-4AE5-9F0F-B0FB203D3CB6">
<name>ProductID</name>
<value>Axis2ChDevice</value>
</setting>
<setting typeid="F9D27CE8-8FA9-4B91-8C85-3E80DE7EF0AB">
<name>MacAddress</name>
<value>ACCC8E5EE927</value>
</setting>
答案 0 :(得分:0)
您可以使用:
WITH cte AS (
SELECT s.c.value('./name[1]', 'NVARCHAR(MAX)') AS name,
s.c.value('./value[1]', 'NVARCHAR(MAX)') AS val
FROM Hardware h
CROSS APPLY h.Settings.nodes('//setting') s(c)
)
SELECT *
FROM cte
WHERE name = 'MacAddress';
<强> DBFiddle Demo 强>
答案 1 :(得分:0)
我使用的where条件有点粗糙但是如果你确定你的架构,那么你可以使用类似下面的东西将MacAddress设置作为一行返回:
declare @hardware xml = '<properties>
<settings hwid="hardware">
<setting typeid="928B6039-4E4A-4B03-9A82-A1CF29748ACC">
<name>DetectedModelName</name>
<value>AXIS P3365 Fixed Dome Network Camera</value>
</setting>
<setting typeid="DAF813F8-2E02-4AE5-9F0F-B0FB203D3CB6">
<name>ProductID</name>
<value>Axis2ChDevice</value>
</setting>
<setting typeid="F9D27CE8-8FA9-4B91-8C85-3E80DE7EF0AB">
<name>MacAddress</name>
<value>ACCC8E5EE927</value>
</setting>
</settings>
</properties>'
select
Hardware.Setting.value('./@typeid', 'nvarchar(50)') as 'TypeId',
Hardware.Setting.value('(./name)[1]', 'nvarchar(50)') as 'SettingName',
Hardware.Setting.value('(./value)[1]', 'nvarchar(50)') as 'SettingValue'
from @hardware.nodes('/properties/settings/setting') as Hardware(Setting)
where Hardware.Setting.value('(./name)[1]', 'nvarchar(50)') = 'MacAddress'