【问题标题】:How to extract values from XML stored as ntext in SQL如何从 SQL 中存储为 ntext 的 XML 中提取值
【发布时间】: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">&lt;?xml version="1.0" encoding="utf-8"?&gt;&lt;pd:Data xmlns:pd="http://www.test.com/XMLSchema"&gt;&lt;pd:Request_Source system_name="Zo5ZCqLRRfV" datatype="object" listID="" listValue=""&gt;Coastal Urban&lt;/pd:Request_Source&gt;&lt;pd:Request_Category system_name="ojZD66m94eu" datatype="object" listID="" listValue=""&gt;Information Request&lt;/pd:Request_Category&gt;&lt;pd:Date_of_Request system_name="KNc4mzCYmYL" datatype="date" listID="" listValue=""&gt;05 Feb 2015&lt;pd:Request_Date system_name="QXf1HQ7EFUZ" datatype="string" listID="" listValue=""&gt;06-Feb-15 03:38:58&lt;/pd:Request_Date&gt;&lt;pd:Request_Priority system_name="" datatype="" listID="" listValue=""&gt;&lt;/pd:Request_Priority&gt;&lt;/pd:Data&gt;</Value>
  </NameValue>
</ArrayOfNameValue>

我正在尝试从中提取元素名称和值

<Value xsi:type="xsd:string">&lt;?xml version="1.0" encoding="utf-8"?&gt;&lt;pd:Data xmlns:pd="http://www.test.com/XMLSchema"&gt;&lt;pd:Request_Source system_name="Zo5ZCqLRRfV" datatype="object" listID="" listValue=""&gt;Coastal Urban&lt;/pd:Request_Source&gt;&lt;pd:Request_Category system_name="ojZD66m94eu" datatype="object" listID="" listValue=""&gt;Information Request&lt;/pd:Request_Category&gt;&lt;pd:Date_of_Request system_name="KNc4mzCYmYL" datatype="date" listID="" listValue=""&gt;05 Feb 2015&lt;pd:Request_Date system_name="QXf1HQ7EFUZ" datatype="string" listID="" listValue=""&gt;06-Feb-15 03:38:58&lt;/pd:Request_Date&gt;&lt;pd:Request_Priority system_name="" datatype="" listID="" listValue=""&gt;&lt;/pd:Request_Priority&gt;&lt;/pd:Data&gt;</Value>

我想提取 Request_Category 是 'Information Request'。

如何使用 XML 方法将它们提取出来并将它们插入到我的查询中的两列中,以便我拥有名称和值?

谢谢

【问题讨论】:

  • ntexttextimage 数据类型将在 SQL Server 的未来版本中删除。避免在新的开发工作中使用这些数据类型,并计划修改当前使用它们的应用程序。请改用nvarchar(max)varchar(max)varbinary(max)See details here
  • 我认为这与另一篇文章完全不同,因为我的问题是所有名称和值都作为单个节点嵌入到 XML 中。下面的答案让我找到了方向,但仍然没有拆分我需要嵌入在 节点中的字段

标签: sql sql-server xml


【解决方案1】:
DECLARE @XML 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">&lt;?xml version="1.0" encoding="utf-8"?&gt;&lt;pd:Data xmlns:pd="http://www.test.com/XMLSchema"&gt;&lt;pd:Request_Source system_name="Zo5ZCqLRRfV" datatype="object" listID="" listValue=""&gt;Coastal Urban&lt;/pd:Request_Source&gt;&lt;pd:Request_Category system_name="ojZD66m94eu" datatype="object" listID="" listValue=""&gt;Information Request&lt;/pd:Request_Category&gt;&lt;pd:Date_of_Request system_name="KNc4mzCYmYL" datatype="date" listID="" listValue=""&gt;05 Feb 2015&lt;pd:Request_Date system_name="QXf1HQ7EFUZ" datatype="string" listID="" listValue=""&gt;06-Feb-15 03:38:58&lt;/pd:Request_Date&gt;&lt;pd:Request_Priority system_name="" datatype="" listID="" listValue=""&gt;&lt;/pd:Request_Priority&gt;&lt;/pd:Data&gt;</Value>
  </NameValue>
</ArrayOfNameValue>'; 

SELECT
bar.value('local-name(.)','VARCHAR(10)') as ColName,  
bar.value('./.','VARCHAR(MAX)')  as Value 
FROM
@xml.nodes('/ArrayOfNameValue/NameValue/*') AS foo(bar)

结果:

ColName    Value
---------- ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Name       Form_Submit Request_3E6791F5B9884EE2B335BC1F1E1285C5
Value      ProjectX-LIB-46c3219d-1fe0-4abb-b559-efb8a3c35505,AgileForm.34
Name       Form
Value      ProjectX-LIB-46c3219d-1fe0-4abb-b559-efb8a3c35505,AgileForm.69
Name       Form_Update Request_E4FA8434326E42ADB6222978214E93E9
Value      ProjectX-LIB-46c3219d-1fe0-4abb-b559-efb8a3c35505,AgileForm.69
Name       //
Value      <?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" da

这就是你要找的吗?如果有的话,我希望这是一个好的开始。

【讨论】:

  • 很好的开始,但问题仍然存在,即字段嵌入在您列为结果的最后一个值行中。有没有办法检索这些字段值? Request_Source=沿海城市
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-10-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-09-05
  • 2020-09-21
相关资源
最近更新 更多