2012-05-09 70 views
0

我無法構建正確的syntaxt,以便在我的sql服務器2008上執行與XML字段相關的任何查詢。XML字段 - 查詢

我有一個簡單的表,如Idproduct,ProductName和XmlProduct。 ..和XMLPRODUCT場看起來像以下:

<DynamicProfile xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/WinTest"> 
    <AllData xmlns:d2p1="http://schemas.microsoft.com/2003/10/Serialization/Arrays"> 
    <d2p1:KeyValueOfstringstring> 
     <d2p1:Key>MyFirstKey</d2p1:Key> 
     <d2p1:Value>MyFirstValue</d2p1:Value> 
    </d2p1:KeyValueOfstringstring> 
    <d2p1:KeyValueOfstringstring> 
     <d2p1:Key>MySecondKey</d2p1:Key> 
     <d2p1:Value>MySecondValue</d2p1:Value> 
    </d2p1:KeyValueOfstringstring> 
    </AllData> 
</DynamicProfile> 

舉例來說,我需要編寫一個查詢其抽取包含節點的值改爲「MySecondKey」的所有行。 如何才能做到這一點?

回答

0

您可以使用下面的代碼TSQL - >

declare @xmldata xml 
    set @xmldata = 
    '<DynamicProfile xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/WinTest"> 
    <AllData xmlns:d2p1="http://schemas.microsoft.com/2003/10/Serialization/Arrays"> 
    <d2p1:KeyValueOfstringstring> 
     <d2p1:Key>MyFirstKey</d2p1:Key> 
     <d2p1:Value>MyFirstValue</d2p1:Value> 
    </d2p1:KeyValueOfstringstring> 
    <d2p1:KeyValueOfstringstring> 
     <d2p1:Key>MySecondKey</d2p1:Key> 
     <d2p1:Value>MySecondValue</d2p1:Value> 
    </d2p1:KeyValueOfstringstring> 
    <d2p1:KeyValueOfstringstring> 
     <d2p1:Key>MySecondKey</d2p1:Key> 
     <d2p1:Value>MySecondValue</d2p1:Value> 
    </d2p1:KeyValueOfstringstring> <d2p1:KeyValueOfstringstring> 
     <d2p1:Key>MyFirstKey</d2p1:Key> 
     <d2p1:Value>MySecondValue</d2p1:Value> 
    </d2p1:KeyValueOfstringstring> <d2p1:KeyValueOfstringstring> 
     <d2p1:Key>MyFirstKey</d2p1:Key> 
     <d2p1:Value>MySecondValue</d2p1:Value> 
    </d2p1:KeyValueOfstringstring> <d2p1:KeyValueOfstringstring> 
     <d2p1:Key>MySecondKey</d2p1:Key> 
     <d2p1:Value>MySecondValue</d2p1:Value> 
    </d2p1:KeyValueOfstringstring> <d2p1:KeyValueOfstringstring> 
     <d2p1:Key>MySecondKey</d2p1:Key> 
     <d2p1:Value>MySecondValue</d2p1:Value> 
    </d2p1:KeyValueOfstringstring> <d2p1:KeyValueOfstringstring> 
     <d2p1:Key>MyFirstKey</d2p1:Key> 
     <d2p1:Value>MySecondValue</d2p1:Value> 
    </d2p1:KeyValueOfstringstring> </AllData> 
</DynamicProfile>' 

DECLARE @hDoc int, @rootxmlns varchar(100) 
SET @rootxmlns = '<AllData xmlns:d2p1="http://schemas.microsoft.com/2003/10/Serialization/Arrays" />' 

EXEC sp_xml_preparedocument @hDoc OUTPUT, @xmldata, @rootxmlns 

SELECT * 
FROM OPENXML(@hDoc, '//d2p1:KeyValueOfstringstring',2) 
WITH ([d2p1:Key] varchar(100) , [d2p1:Value] varchar(100)) where [d2p1:Key] = 'MySecondKey' 

--clean up 
EXEC sp_xml_removedocument @hDoc 
+0

爲什麼我需要聲明@xmldata變量?這已經存在於我的表xml字段中。另外,有沒有使用XQuery的解決方案?像查詢,價值,存在的方法..? – bit

+0

與xquery類似的解決方案在這裏推薦 - > http://stackoverflow.com/questions/1440848/openxml-with-xmlnsdt – rt2800

+0

請你能給我發一個與我的問題有關的明確例子嗎? – bit

1

下面是使用XQuery運營商俺們 - >

declare @xmldata xml 
    set @xmldata = 
    '<DynamicProfile xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/WinTest"> 
    <AllData xmlns:d2p1="http://schemas.microsoft.com/2003/10/Serialization/Arrays"> 
    <d2p1:KeyValueOfstringstring> 
     <d2p1:Key>MyFirstKey</d2p1:Key> 
     <d2p1:Value>MyFirstValue</d2p1:Value> 
    </d2p1:KeyValueOfstringstring> 
    <d2p1:KeyValueOfstringstring> 
     <d2p1:Key>MySecondKey</d2p1:Key> 
     <d2p1:Value>MySecondValue</d2p1:Value> 
    </d2p1:KeyValueOfstringstring> 
    <d2p1:KeyValueOfstringstring> 
     <d2p1:Key>MySecondKey</d2p1:Key> 
     <d2p1:Value>MySecondValue</d2p1:Value> 
    </d2p1:KeyValueOfstringstring> <d2p1:KeyValueOfstringstring> 
     <d2p1:Key>MyFirstKey</d2p1:Key> 
     <d2p1:Value>MySecondValue</d2p1:Value> 
    </d2p1:KeyValueOfstringstring> <d2p1:KeyValueOfstringstring> 
     <d2p1:Key>MyFirstKey</d2p1:Key> 
     <d2p1:Value>MySecondValue</d2p1:Value> 
    </d2p1:KeyValueOfstringstring> <d2p1:KeyValueOfstringstring> 
     <d2p1:Key>MySecondKey</d2p1:Key> 
     <d2p1:Value>MySecondValue</d2p1:Value> 
    </d2p1:KeyValueOfstringstring> <d2p1:KeyValueOfstringstring> 
     <d2p1:Key>MySecondKey</d2p1:Key> 
     <d2p1:Value>MySecondValue</d2p1:Value> 
    </d2p1:KeyValueOfstringstring> <d2p1:KeyValueOfstringstring> 
     <d2p1:Key>MyFirstKey</d2p1:Key> 
     <d2p1:Value>MySecondValue</d2p1:Value> 
    </d2p1:KeyValueOfstringstring> </AllData> 
</DynamicProfile>' 

;WITH XMLNAMESPACES 
(
    DEFAULT 'http://schemas.datacontract.org/2004/07/WinTest', 
    'http://schemas.microsoft.com/2003/10/Serialization/Arrays' as d2p1 
) 
SELECT x.c.value('(d2p1:Key)[1]', 'varchar(100)') as key3, x.c.value('(d2p1:Value)[1]', 'varchar(100)') as value 
FROM @xmldata.nodes('/DynamicProfile/AllData/d2p1:KeyValueOfstringstring') x(c) where x.c.value('(d2p1:Key)[1]', 'varchar(100)') = 'MySecondKey' 
+0

那麼,但在我的情況下,@ xmldata變量是一個表字段。所以,考慮你想執行一個查詢來提取所有與xml匹配的行,在那裏clausole,我該怎麼做? – bit

+0

請查看本文 - > http://www.databasejournal.com/features/mssql/article.php/3674311/Query-XML-data-from-a-table-with-XML-data-type.htm – rt2800

+0

什麼都不是去做。對我來說,「翻譯」xml字段並不重要,相反,我只能檢索受我的where條件影響的所有行。哪裏condifition指的是xml字段,但最終我想實現的是「本地」行,例如:Select * from MyTable WHERE XmlField.Value(key)='some value' – bit