2012-05-09 68 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