18個小實例入門SQLServer XML查詢


/*
sql xml 入門:
    --by Erichu
    --http://blog.csdn.net/chinahuyong
    
    1、xml:        能認識元素、屬性和值
    
    2、xpath:    尋址語言,類似windows目錄的查找(沒用過dir命令的話就去面壁)
                
                語法格式,這些語法可以組合為條件:
                "."表示自己,".."表示父親,"/"表示兒子,"//"表示后代,
                "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 xml
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 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的(第一個)title
select @data.query('//book[./title[@lang="en"] and price>35 ]').value('(book/title)[1]','varchar(max)')
--13、等價於12
select @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、獲取第一本書的title
select Tab.Col.value('(book/title)[1]','varchar(max)') as title
    from @data.nodes('bookstore')as Tab(Col) 
--16、獲取每本書的第一個author
select Tab.Col.value('author[1]','varchar(max)') as title
    from @data.nodes('//book')as Tab(Col)
--17、獲取所有book的所有信息
select
 T.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 author4
from @data.nodes('//book') as T(C)
--18、獲取不是日語(lang!="jp")且價格大於35的書的所有信息
select
 T.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 author4
from @data.nodes('//book[./title[@lang!="jp"] and price>35 ]') as T(C)

 

 
       


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM