sql xml 入門

來源:互聯網
上載者:User

標籤:

/*sql xml 入門: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)

 來源:http://www.jb51.net/article/19659.htm

sql xml 入門

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.