Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server 如何在SQLServer2008中查询XML列_Sql Server_Xml - Fatal编程技术网

Sql server 如何在SQLServer2008中查询XML列

Sql server 如何在SQLServer2008中查询XML列,sql-server,xml,Sql Server,Xml,我正在使用SQLServer2008 我有一个包含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">

我正在使用SQLServer2008

我有一个包含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>  
如何创建查询以及如何构建xml索引


非常感谢。

要选择具有您提到的条件的所有行,请尝试此select语句:

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索引,请阅读如何创建和使用它们。

如果要发布代码或XML,请突出显示这些行并单击编辑器工具栏上的“代码”按钮(101 010)。这样可以很好地格式化代码/XML,并提供语法突出显示,而且您不必乱动XML代码(无需替换<和>字符)!不要使用-在这里不起作用,非常感谢。根据您的信息,我创建了如下索引:在TABLEA上创建主XML索引idx_xCol(XML_col)在TABLEA上创建XML索引idx_xCol_路径(XML_col)使用XML索引idx_xCol作为路径在TABLEA上创建XML索引idx_xCol_值(XML_col)使用XML索引idx_xCol作为值,但性能看起来不是很好,对索引有什么建议吗?@envykok:如果您对所有内容都有一个XML索引,那么这就是您所能做的。使用XQuery访问XML并不是一个非常快的操作。。。。。如果需要反复查询某些元素,请将它们提取到关系表结构中,而不是将它们放在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'