Sql server 如何解析SQL Server中存储在列中的XML
我想解析存储在SQL Server列中的XML: XML是:Sql server 如何解析SQL Server中存储在列中的XML,sql-server,xml,parsing,Sql Server,Xml,Parsing,我想解析存储在SQL Server列中的XML: XML是: <?xml version="1.0" encoding="UTF-8"?> <propertylist id="root"> <property expanded="Y" id="createsdi" selectedindex="0" type="
<?xml version="1.0" encoding="UTF-8"?>
<propertylist id="root">
<property expanded="Y" id="createsdi" selectedindex="0" type="collection">
<collection>
<propertylist id="1305619640064">
<property expanded="Y" id="columnvalues" selectedindex="0" type="collection">
<collection>
<propertylist id="1396440519721" />
</collection>
</property>
<property expanded="Y" id="tests" selectedindex="0" type="collection">
<collection>
<propertylist id="p1602059752707" sequence="1000000">
<property id="id" type="simple"><![CDATA[item1]]></property>
<property id="workitemid" type="simple"><![CDATA[SOURAVTESTER|1]]></property>
</propertylist>
</collection>
</property>
<property expanded="Y" id="specs" selectedindex="0" type="collection">
<collection>
<propertylist id="p1602059825237" sequence="1000000">
<property id="id" type="simple"><![CDATA[item1]]></property>
<property id="specid" type="simple"><![CDATA[EMSpec|1]]></property>
</propertylist>
</collection>
</property>
</propertylist>
</collection>
</property>
<property expanded="Y" id="workorder" type="propertylist">
<propertylist id="root_0">
<property expanded="Y" id="graceperiod" type="propertylist">
<propertylist id="root_0_workorder_0">
<property expanded="Y" id="graceperiod" type="propertylist">
<propertylist id="root_0_workorder_0_graceperiod_0" />
</property>
<property expanded="Y" id="deviation" type="propertylist">
<propertylist id="root_0_workorder_0_graceperiod_0" />
</property>
</propertylist>
</property>
</propertylist>
</property>
</propertylist>
但我犯了个错误
XMLDT方法“节点”只能在xml类型的列上调用
结果应该是:
workitemid SOURAVTESTER
specid EMSpec
请尝试以下解决方案 它做了以下工作:
- 删除使用UTF-8编码的XML prolog。SQLServer隐式地将任何XML在内部转换为UTF-16
- 将NVARCHAR()数据类型转换为XML数据类型。在那之后,XQuery 处理XML的方法变得可用
-- DDL and sample data population, start
DECLARE @tbl TABLE (ID INT IDENTITY PRIMARY KEY, col NVARCHAR(MAX));
INSERT INTO @tbl (col) VALUES
(N'<?xml version="1.0" encoding="UTF-8"?>
<propertylist id="root">
<property expanded="Y" id="createsdi" selectedindex="0" type="collection">
<collection>
<propertylist id="1305619640064">
<property expanded="Y" id="columnvalues" selectedindex="0"
type="collection">
<collection>
<propertylist id="1396440519721"/>
</collection>
</property>
<property expanded="Y" id="tests" selectedindex="0"
type="collection">
<collection>
<propertylist id="p1602059752707" sequence="1000000">
<property id="id" type="simple"><![CDATA[item1]]></property>
<property id="workitemid" type="simple"><![CDATA[SOURAVTESTER|1]]></property>
</propertylist>
</collection>
</property>
<property expanded="Y" id="specs" selectedindex="0"
type="collection">
<collection>
<propertylist id="p1602059825237" sequence="1000000">
<property id="id" type="simple"><![CDATA[item1]]></property>
<property id="specid" type="simple"><![CDATA[EMSpec|1]]></property>
</propertylist>
</collection>
</property>
</propertylist>
</collection>
</property>
<property expanded="Y" id="workorder" type="propertylist">
<propertylist id="root_0">
<property expanded="Y" id="graceperiod" type="propertylist">
<propertylist id="root_0_workorder_0">
<property expanded="Y" id="graceperiod" type="propertylist">
<propertylist id="root_0_workorder_0_graceperiod_0"/>
</property>
<property expanded="Y" id="deviation" type="propertylist">
<propertylist id="root_0_workorder_0_graceperiod_0"/>
</property>
</propertylist>
</property>
</propertylist>
</property>
</propertylist>')
, (NULL);
-- DDL and sample data population, end
--DECLARE @startPos INT = LEN('<?xml version="1.0" encoding="utf-8"?>') + 1;
DECLARE @prolog NVARCHAR(100) = '<?xml version="1.0" encoding="utf-8"?>';
;WITH rs AS
(
SELECT ID
--, TRY_CAST(SUBSTRING(col
-- , @startPos
-- , LEN(col)) AS XML) AS xmldata
, TRY_CAST('<root>' + REPLACE(col, @prolog, '') + '</root>' AS XML) AS xmldata FROM @tbl
)
SELECT ID
, c.value('(./text())[1]', 'VARCHAR(100)') AS Item
FROM rs
CROSS APPLY xmldata.nodes('//property[@id=("specid","workitemid")]') AS t(c);
请编辑您的文章并指定所需的输出。有一列包含所有XML。我必须检查所有XML并找到值。请检查描述中附带的图像。@sourav,您有责任提供DDL和样本数据填充。不是IT的图片。@sourav,我调整了答案。'Table column with XML在此图片中'-请检查上面的链接,先生。@sourav,我调整了答案。过来看。
-- DDL and sample data population, start
DECLARE @tbl TABLE (ID INT IDENTITY PRIMARY KEY, col NVARCHAR(MAX));
INSERT INTO @tbl (col) VALUES
(N'<?xml version="1.0" encoding="UTF-8"?>
<propertylist id="root">
<property expanded="Y" id="createsdi" selectedindex="0" type="collection">
<collection>
<propertylist id="1305619640064">
<property expanded="Y" id="columnvalues" selectedindex="0"
type="collection">
<collection>
<propertylist id="1396440519721"/>
</collection>
</property>
<property expanded="Y" id="tests" selectedindex="0"
type="collection">
<collection>
<propertylist id="p1602059752707" sequence="1000000">
<property id="id" type="simple"><![CDATA[item1]]></property>
<property id="workitemid" type="simple"><![CDATA[SOURAVTESTER|1]]></property>
</propertylist>
</collection>
</property>
<property expanded="Y" id="specs" selectedindex="0"
type="collection">
<collection>
<propertylist id="p1602059825237" sequence="1000000">
<property id="id" type="simple"><![CDATA[item1]]></property>
<property id="specid" type="simple"><![CDATA[EMSpec|1]]></property>
</propertylist>
</collection>
</property>
</propertylist>
</collection>
</property>
<property expanded="Y" id="workorder" type="propertylist">
<propertylist id="root_0">
<property expanded="Y" id="graceperiod" type="propertylist">
<propertylist id="root_0_workorder_0">
<property expanded="Y" id="graceperiod" type="propertylist">
<propertylist id="root_0_workorder_0_graceperiod_0"/>
</property>
<property expanded="Y" id="deviation" type="propertylist">
<propertylist id="root_0_workorder_0_graceperiod_0"/>
</property>
</propertylist>
</property>
</propertylist>
</property>
</propertylist>')
, (NULL);
-- DDL and sample data population, end
--DECLARE @startPos INT = LEN('<?xml version="1.0" encoding="utf-8"?>') + 1;
DECLARE @prolog NVARCHAR(100) = '<?xml version="1.0" encoding="utf-8"?>';
;WITH rs AS
(
SELECT ID
--, TRY_CAST(SUBSTRING(col
-- , @startPos
-- , LEN(col)) AS XML) AS xmldata
, TRY_CAST('<root>' + REPLACE(col, @prolog, '') + '</root>' AS XML) AS xmldata FROM @tbl
)
SELECT ID
, c.value('(./text())[1]', 'VARCHAR(100)') AS Item
FROM rs
CROSS APPLY xmldata.nodes('//property[@id=("specid","workitemid")]') AS t(c);
+----+----------------+
| ID | Item |
+----+----------------+
| 1 | SOURAVTESTER|1 |
| 1 | EMSpec|1 |
+----+----------------+