xml.query()函数,XQuery表达式可以返回xml一个单一的元素或是所有元素。
我们继续使用上一篇所创建表[dbo].[tbDevLanguage] http://www.cnblogs.com/insus/archive/2012/02/25/2367668.html
首先我们演示Query所有元素,注意一下查询节点,返回结果获取范围:
SELECT [languages].query('/Languages') AS [language]
FROM [dbo].[tbDevLanguage]
SELECT [languages].query('/Languages/language') AS [language]
FROM [dbo].[tbDevLanguage]
FROM [dbo].[tbDevLanguage]
SELECT [languages].query('/Languages/language') AS [language]
FROM [dbo].[tbDevLanguage]
上两sql语句执行结果:
最后再演示一下返回一个元素:
SELECT [languages].query('/Languages[1]/language[1]') AS [language]
FROM [dbo].[tbDevLanguage]
SELECT [languages].query('/Languages[1]/language[2]') AS [language]
FROM [dbo].[tbDevLanguage]
SELECT [languages].query('/Languages[1]/language[3]') AS [language]
FROM [dbo].[tbDevLanguage]
FROM [dbo].[tbDevLanguage]
SELECT [languages].query('/Languages[1]/language[2]') AS [language]
FROM [dbo].[tbDevLanguage]
SELECT [languages].query('/Languages[1]/language[3]') AS [language]
FROM [dbo].[tbDevLanguage]
上面三句SQL语句,分别取得三个元素: