你的位置:首页 > 数据库

[数据库]Sql Xml


/*sql http://blog.csdn.net/jinjazz    1、*/declare @data set @data='<bookstore><book category="COOKING"> <title lang="en">Everyday Italian</title> <author>Giada De Laurentiis</author> <year>2005</year> <price>30.00</price></book><book category="CHILDREN"> <title lang="jp">Harry Potter</title> <author>J K. Rowling</author> <year>2005</year> <price>29.99</price></book><book category="WEB"> <title lang="en">XQuery Kick Start</title> <author>James McGovern</author> <author>Per Bothner</author> <author>Kurt Cagle</author> <author>James Linn</author> <author>Vaidyanathan Nagarajan</author> <year>2003</year> <price>49.99</price></book><book category="WEB"> <title lang="cn">Learning 2003</year> <price>39.95</price></book></bookstore>'--测试语句,如果不理解语法请参考上面的xpath规则和xquery函数说明--1、文档select @data--2、任意级别是否存在price节点select @data.exist('//price')--3、获取所有book节点select @data.query('//book')--4、获取所有包含lang属性的节点select @data.query('//*[@lang]') --5、获取第一个book节点select @data.query('//book[1]')--6、获取前两个book节点select @data.query('//book[position()<=2]')--7、获取最后一个book节点select @data.query('//book[last()]')--8、获取price>35的所有book节点select @data.query('//book[price>35]')--9、获取category="WEB"的所有book节点select @data.query('//book[@category="WEB"]')--10、获取title的lang="en"的所有book节点select @data.query('//book/title[@lang="en"]')--11、获取title的lang="en"且 price>35的所有book节点select @data.query('//book[./title[@lang="en"] or price>35 ]')--12、获取title的lang="en"且 price>35的第一book的(第一个)titleselect @data.query('//book[./title[@lang="en"] and price>35 ]').value('(book/title)[1]','varchar(max)')--13、等价于12select @data.value('(//book[./title[@lang="en"] and price>35 ]/title)[1]','varchar(max)')--14、获取title的lang="en"且 price>35的第一book的(第一个)title的lang属性select @data.value('((//book[@category="WEB" and price>35 ]/title)[1]/@lang)[1]','varchar(max)')--15、获取第一本书的titleselect Tab.Col.value('(book/title)[1]','varchar(max)') as title  from @data.nodes('bookstore')as Tab(Col) --16、获取每本书的第一个authorselect Tab.Col.value('author[1]','varchar(max)') as title  from @data.nodes('//book')as Tab(Col)--17、获取所有book的所有信息selectT.C.value('title[1]','varchar(max)') as title,T.C.value('year[1]','int') as year,T.C.value('title[1]','varchar(max)')as title,T.C.value('price[1]','float') as price,T.C.value('author[1]','varchar(max)') as author1,T.C.value('author[2]','varchar(max)') as author2,T.C.value('author[3]','varchar(max)') as author3,T.C.value('author[4]','varchar(max)') as author4from @data.nodes('//book') as T(C)--18、获取不是日语(lang!="jp")且价格大于35的书的所有信息selectT.C.value('title[1]','varchar(max)') as title,T.C.value('year[1]','int') as year,T.C.value('title[1]','varchar(max)')as title,T.C.value('price[1]','float') as price,T.C.value('author[1]','varchar(max)') as author1,T.C.value('author[2]','varchar(max)') as author2,T.C.value('author[3]','varchar(max)') as author3,T.C.value('author[4]','varchar(max)') as author4from @data.nodes('//book[./title[@lang!="jp"] and price>35 ]') as T(C)

记录一些学习的知识。

http://www.cnblogs.com/kenshincui/archive/2011/12/31/2309217.html