如何从SQL中存储为ntext的XML中提取值

时间:2015-03-07 01:27:00

标签: sql sql-server xml

我有一个数据库,我试图在我们的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是&#39;信息请求&#39;。

如何使用XML方法将它们拉出来并在查询中将它们插入两列,这样我就有了Name和Value?

由于

1 个答案:

答案 0 :(得分:0)

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

这是你在找什么?如果有的话,我希望它是一个良好的开端。