/*sql xml 入門: --by jinjazz --http://blog.csdn.net/jinjazz 1、xml: 能認識元素、屬性和值 2、xpath: 尋址語言,類似windows目錄的查找(沒用過dir命令的話就去面壁) 語法格式,這些語法可以組合為條件: "."表示自己,".."表示父親,"/"表示兒子,"http://"表示后代, "name"表示按名字查找,"@name"表示按屬性查找 "集合[條件]" 表示根據條件取集合的子集,條件可以是 數 值:數字,last(),last()-數字 等 布爾值:position()<數字,@name='條件',name='條件' 條件是布爾值的時候可以合并計算:and or 3、xquery: 基于xpath標的準查詢語言,sqlserver xquery包含如下函數 exist(xpath條件):返回布爾值表示節點是否存在 query(xpath條件):返回由符合條件的節點組成的新的xml文檔 value(xpath條件,數據類型):返回指定的標量值,xpath條件結果必須唯一 nodes(xpath條件): 返回由符合條件的節點組成的一行一列的結果表*/declare @data xmlset @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 XML</title> <author>Erik T. Ray</author> <year>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
新聞熱點
疑難解答