2012-07-03 66 views
3

我有類似下面的[XMLValue]一個名爲「AlgorithmLog」表列內碼:如何查詢SQL Server中的XML列中的XML值

<?xml version="1.0" encoding="utf-8"?> 
<AdapterInfo xmlns:i="http://www.w3.org/2001/XMLSchema-instance"  
      xmlns:d1p1="http://schemas.datacontract.org/2004/07/Adapters.Adapter.CloudTrader"  
      xmlns="http://schemas.datacontract.org/2004/07/Adapters.Adapter" 
      i:type="d1p1:AlgorithmStatusReport"> 
    <SequenceNumber>0</SequenceNumber> 
    <TrackingGuid i:nil="true" /> 
    <d1p1:Broker>Default</d1p1:Broker> 
    ... 
     <d1p1:XMLValue>&lt;?xml version="1.0"?&gt;&lt;int xmlns="http://schemas.microsoft.com/2003/10/Serialization/"&gt;1900&lt;/int&gt;</d1p1:XMLValue> 
</AdapterInfo> 

,我想要得到的值「1900 「在節點內<d1p1:XMLValue>

因此,這裏是我的查詢:

WITH XMLNAMESPACES('http://schemas.datacontract.org/2004/07/Adapters.Adapter' AS x, 
        'http://schemas.datacontract.org/2004/07/Adapters.Adapter.CloudTrader' As p, 
        'http://schemas.microsoft.com/2003/10/Serialization/'as w) 
    SELECT 
     XMLValue.query('(/x:AdapterInfo/p:XMLValue/w:int)[1]')AS [XMLVaule] 
    FROM AlgorithmLog 

但它沒有返回。

有誰能告訴我我做錯了什麼或我該怎麼做嗎?

謝謝。

回答

2

既然你已經「編碼」 XML另一個XML節點內,並且不能自動轉換爲使用.value() XQuery的方法XML數據類型,這一切都變得複雜一些 - 但是這似乎爲我工作:

;WITH XMLNAMESPACES('http://schemas.datacontract.org/2004/07/Adapters.Adapter' AS x, 
        'http://schemas.datacontract.org/2004/07/Adapters.Adapter.CloudTrader' As p, 
        'http://schemas.microsoft.com/2003/10/Serialization/'as w) 
    SELECT 
     CAST(XmlContent.value('(/x:AdapterInfo/p:XMLValue)[1]', 'varchar(2000)') AS XML).value('(w:int)[1]', 'int') AS [XMLValue] 
    FROM AlgorithmLog 
    WHERE ....... -- use whatever condition makes sense for you here 
+1

這工作完全正常。非常感謝! – Lillian