[英]Extract node value from an xml from a table using SQL
<Recording xmlns="http://www.m5net.com/test/configuration/connectors" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<Id>16607</Id>
<Mode>1</Mode>
<Enable>true</Enable>
<Notification i:nil="true" />
<Notify>false</Notify>
</Recording>
我需要提取模式的值。 這是 ModeConfiguration 表中的配置列。
我努力了
SELECT
config.Value('(/Recording//Mode)[1]', 'varchar(max)') as int
FROM ModeConfiguration
但看起來命名空間沒有得到正確考慮。
應考慮默認命名空間。
SQL
-- DDL and sample data population, start
DECLARE @ModeConfiguration TABLE (ID INT IDENTITY PRIMARY KEY, config XML);
INSERT INTO @ModeConfiguration (config) VALUES
(N'<Recording xmlns="http://www.m5net.com/test/configuration/connectors"
xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<Id>16607</Id>
<Mode>1</Mode>
<Enable>true</Enable>
<Notification i:nil="true"/>
<Notify>false</Notify>
</Recording>');
-- DDL and sample data population, end
;WITH XMLNAMESPACES (DEFAULT 'http://www.m5net.com/test/configuration/connectors')
SELECT c.value('(Id/text())[1]', 'INT') AS Id
, c.value('(Mode/text())[1]', 'INT') AS Mode
FROM @ModeConfiguration
CROSS APPLY config.nodes('/Recording') AS t(c);
Output
+-------+------+
| Id | Mode |
+-------+------+
| 16607 | 1 |
+-------+------+
關於類型化 XML 主題的良好鏈接: 類型化 XML 與非類型化 XML 的文本()困境
這有效:
select t.node.value('*:Mode[1]', 'int')
from ModeConfiguration
CROSS APPLY Config.nodes('*:Recording') t(node)
WHERE featurei=27;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.