【发布时间】:2015-03-07 01:27:00
【问题描述】:
我有一个数据库,我试图在其中查询在我们的 SQL Server 中存储为 ntext 的 XML 的列。为什么它不存储为 XML?不确定,但我无法更改它,所以我努力尝试将其转换为某种 XML 格式。此时我已经能够运行它来获取被视为 XML 的条目行。
select top 5 cast(cast(ATTR_XML as varchar(max)) as XML)
from dbo.table1
这导致以下内容被识别为 XML
<ArrayOfNameValue xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<NameValue>
<Name>Form_Submit Request_3E6791F5B9884EE2B335BC1F1E1285C5</Name>
<Value xsi:type="xsd:string">ProjectX-LIB-46c3219d-1fe0-4abb-b559-efb8a3c35505,AgileForm.34</Value>
</NameValue>
<NameValue>
<Name>Form</Name>
<Value xsi:type="xsd:string">ProjectX-LIB-46c3219d-1fe0-4abb-b559-efb8a3c35505,AgileForm.69</Value>
</NameValue>
<NameValue>
<Name>Form_Update Request_E4FA8434326E42ADB6222978214E93E9</Name>
<Value xsi:type="xsd:string">ProjectX-LIB-46c3219d-1fe0-4abb-b559-efb8a3c35505,AgileForm.69</Value>
</NameValue>
<NameValue>
<Name>//</Name>
<Value xsi:type="xsd:string"><?xml version="1.0" encoding="utf-8"?><pd:Data xmlns:pd="http://www.test.com/XMLSchema"><pd:Request_Source system_name="Zo5ZCqLRRfV" datatype="object" listID="" listValue="">Coastal Urban</pd:Request_Source><pd:Request_Category system_name="ojZD66m94eu" datatype="object" listID="" listValue="">Information Request</pd:Request_Category><pd:Date_of_Request system_name="KNc4mzCYmYL" datatype="date" listID="" listValue="">05 Feb 2015<pd:Request_Date system_name="QXf1HQ7EFUZ" datatype="string" listID="" listValue="">06-Feb-15 03:38:58</pd:Request_Date><pd:Request_Priority system_name="" datatype="" listID="" listValue=""></pd:Request_Priority></pd:Data></Value>
</NameValue>
</ArrayOfNameValue>
我正在尝试从中提取元素名称和值
<Value xsi:type="xsd:string"><?xml version="1.0" encoding="utf-8"?><pd:Data xmlns:pd="http://www.test.com/XMLSchema"><pd:Request_Source system_name="Zo5ZCqLRRfV" datatype="object" listID="" listValue="">Coastal Urban</pd:Request_Source><pd:Request_Category system_name="ojZD66m94eu" datatype="object" listID="" listValue="">Information Request</pd:Request_Category><pd:Date_of_Request system_name="KNc4mzCYmYL" datatype="date" listID="" listValue="">05 Feb 2015<pd:Request_Date system_name="QXf1HQ7EFUZ" datatype="string" listID="" listValue="">06-Feb-15 03:38:58</pd:Request_Date><pd:Request_Priority system_name="" datatype="" listID="" listValue=""></pd:Request_Priority></pd:Data></Value>
我想提取 Request_Category 是 'Information Request'。
如何使用 XML 方法将它们提取出来并将它们插入到我的查询中的两列中,以便我拥有名称和值?
谢谢
【问题讨论】:
-
ntext、text和image数据类型将在 SQL Server 的未来版本中删除。避免在新的开发工作中使用这些数据类型,并计划修改当前使用它们的应用程序。请改用nvarchar(max)、varchar(max)和varbinary(max)。 See details here -
我认为这与另一篇文章完全不同,因为我的问题是所有名称和值都作为单个节点嵌入到 XML 中。下面的答案让我找到了方向,但仍然没有拆分我需要嵌入在
节点中的字段
标签: sql sql-server xml