2010-03-08 191 views
2

我是新来查询SQL Server 2005中的XML数据类型。任何人都可以帮我创建一个查询我的需求?这是我的专栏的一个场景。SQL Server 2005 XML查询

Column Name: Cabinet 

/*Row 1 XML Data*/ 
<shelf> 
<box> 
    <item type="pencil" color="blue"/> 
    <item type="pencil" color="red"/> 
    <item type="paper" color="white"/> 
    <item type="ribbon" color="red"/> 
</box> 
<shelf> 

/*Row 2 XML Data*/ 
<shelf> 
    <item type="pencil" color="yellow"/> 
    <item type="can" color="blue"/> 
    <item type="scissor" color="yellow"/> 
<shelf> 

Desired Output: 
4 
3 

我想统计“项目”节点的数量,而不管其类型为&颜色。提前致谢。

回答

2

看一看这个(全部工作示例

DECLARE @Table TABLE(
     Cabinet XML 
) 

INSERT INTO @Table SELECT 
'<shelf> 
<box> 
    <item type="pencil" color="blue"/> 
    <item type="pencil" color="red"/> 
    <item type="paper" color="white"/> 
    <item type="ribbon" color="red"/> 
</box> 
</shelf>' 

INSERT INTO @Table SELECT 
'<shelf>  
    <item type="pencil" color="yellow"/> 
    <item type="can" color="blue"/> 
    <item type="scissor" color="yellow"/> 
</shelf>' 

SELECT *, 
     Cabinet.query('count(//item)').value('.','int') 
FROM @Table 
+0

感谢,正是我需要的。 – hallie 2010-03-08 15:19:51

+0

这是一个很好的例子! – 2010-04-29 15:20:15