我正在使用 SQL Server 2008
我有一个包含 xml 数据的表,如下所示:
ID xml_col
1 <Attribute id="7">1.0</Attribute><Attribute id="8">AAA</Attribute>
2 <Attribute id="7">2.0</Attribute><Attribute id="8">BBB</Attribute>
3 <Attribute id="7">3.0</Attribute><Attribute id="8">AAA</Attribute>
4 <Attribute id="7">1.0</Attribute><Attribute id="8">BBB</Attribute>
5 <Attribute id="7">1.0</Attribute><Attribute id="8">AAA</Attribute>
6 <Attribute id="7">7.0</Attribute><Attribute id="8">CCC</Attribute>
我想得到那些记录
'when attribute=17 then value =1'+ 'when attribute=8 then value='AAA''
如何创建查询以及如何构建 xml 索引?
非常感谢。
最佳答案
要选择符合您提到的条件的所有行,请尝试此选择语句:
SELECT *
FROM dbo.YourXmlTable
WHERE
YourXmlTable.xml_col.value('(//Attribute[@id=7])[1]', 'decimal') = 1.0
AND
YourXmlTable.xml_col.value('(//Attribute[@id=8])[1]', 'varchar(10)') = 'AAA'
对于 XML 索引,请阅读 Books Online如何创建它们以及如何使用它们。
关于sql-server - 如何在 SQL Server 2008 中查询 XML 列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2988122/