SQLServer XML查詢快速入門(18句話)
更新時間:2009年08月15日 01:59:31 作者:
sql xml 入門,需要的朋友可以參考下。
sql xml 入門: --by jinjazz
1、xml: 能認(rèn)識元素、屬性和值
2、xpath: 尋址語言,類似windows目錄的查找(沒用過dir命令的話就去面壁)
語法格式,這些語法可以組合為條件:
"."表示自己,".."表示父親,"/"表示兒子,"http://"表示后代,
"name"表示按名字查找,"@name"表示按屬性查找
"集合[條件]" 表示根據(jù)條件取集合的子集,條件可以是
數(shù) 值:數(shù)字,last(),last()-數(shù)字 等
布爾值:position()<數(shù)字,@name='條件',name='條件'
條件是布爾值的時候可以合并計算:and or
3、xquery: 基于xpath標(biāo)的準(zhǔn)查詢語言,sqlserver xquery包含如下函數(shù)
exist(xpath條件):返回布爾值表示節(jié)點(diǎn)是否存在
query(xpath條件):返回由符合條件的節(jié)點(diǎn)組成的新的xml文檔
value(xpath條件,數(shù)據(jù)類型):返回指定的標(biāo)量值,xpath條件結(jié)果必須唯一
nodes(xpath條件): 返回由符合條件的節(jié)點(diǎn)組成的一行一列的結(jié)果表
*/
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規(guī)則和xquery函數(shù)說明
--1、文檔
select @data
--2、任意級別是否存在price節(jié)點(diǎn)
select @data.exist('//price')
--3、獲取所有book節(jié)點(diǎn)
select @data.query('//book')
--4、獲取所有包含lang屬性的節(jié)點(diǎn)
select @data.query('//*[@lang]')
--5、獲取第一個book節(jié)點(diǎn)
select @data.query('//book[1]')
--6、獲取前兩個book節(jié)點(diǎn)
select @data.query('//book[position()<=2]')
--7、獲取最后一個book節(jié)點(diǎn)
select @data.query('//book[last()]')
--8、獲取price>35的所有book節(jié)點(diǎn)
select @data.query('//book[price>35]')
--9、獲取category="WEB"的所有book節(jié)點(diǎn)
select @data.query('//book[@category="WEB"]')
--10、獲取title的lang="en"的所有book節(jié)點(diǎn)
select @data.query('//book/title[@lang="en"]')
--11、獲取title的lang="en"且 price>35的所有book節(jié)點(diǎn)
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)
1、xml: 能認(rèn)識元素、屬性和值
2、xpath: 尋址語言,類似windows目錄的查找(沒用過dir命令的話就去面壁)
語法格式,這些語法可以組合為條件:
"."表示自己,".."表示父親,"/"表示兒子,"http://"表示后代,
"name"表示按名字查找,"@name"表示按屬性查找
"集合[條件]" 表示根據(jù)條件取集合的子集,條件可以是
數(shù) 值:數(shù)字,last(),last()-數(shù)字 等
布爾值:position()<數(shù)字,@name='條件',name='條件'
條件是布爾值的時候可以合并計算:and or
3、xquery: 基于xpath標(biāo)的準(zhǔn)查詢語言,sqlserver xquery包含如下函數(shù)
exist(xpath條件):返回布爾值表示節(jié)點(diǎn)是否存在
query(xpath條件):返回由符合條件的節(jié)點(diǎn)組成的新的xml文檔
value(xpath條件,數(shù)據(jù)類型):返回指定的標(biāo)量值,xpath條件結(jié)果必須唯一
nodes(xpath條件): 返回由符合條件的節(jié)點(diǎn)組成的一行一列的結(jié)果表
*/
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規(guī)則和xquery函數(shù)說明
--1、文檔
select @data
--2、任意級別是否存在price節(jié)點(diǎn)
select @data.exist('//price')
--3、獲取所有book節(jié)點(diǎn)
select @data.query('//book')
--4、獲取所有包含lang屬性的節(jié)點(diǎn)
select @data.query('//*[@lang]')
--5、獲取第一個book節(jié)點(diǎn)
select @data.query('//book[1]')
--6、獲取前兩個book節(jié)點(diǎn)
select @data.query('//book[position()<=2]')
--7、獲取最后一個book節(jié)點(diǎn)
select @data.query('//book[last()]')
--8、獲取price>35的所有book節(jié)點(diǎn)
select @data.query('//book[price>35]')
--9、獲取category="WEB"的所有book節(jié)點(diǎn)
select @data.query('//book[@category="WEB"]')
--10、獲取title的lang="en"的所有book節(jié)點(diǎn)
select @data.query('//book/title[@lang="en"]')
--11、獲取title的lang="en"且 price>35的所有book節(jié)點(diǎn)
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)
相關(guān)文章
SQL Server誤區(qū)30日談 第7天 一個實例多個鏡像和日志傳送延遲
這個誤區(qū)就有點(diǎn)老生常談了。每一個主體服務(wù)器只允許一個鏡像服務(wù)器。如果你希望存在多個主體服務(wù)器的副本,那么請使用事務(wù)日志傳送,事務(wù)日志傳送允許針對每一個主體存在多個輔助實例2013-01-01
SQL Server實現(xiàn)split函數(shù)分割字符串功能及用法示例
這篇文章主要介紹了SQL Server實現(xiàn)split函數(shù)分割字符串功能及用法,結(jié)合實例形式分析了SQL Server實現(xiàn)split分割字符串的相關(guān)技巧與使用方法,需要的朋友可以參考下2016-08-08
SQLServer獲取臨時表所有列名或是否存在指定列名的方法
本文介紹了SQLServer獲取臨時表所有列名或是否存在指定列名的方法,需要的朋友一起來看下吧2016-12-12
SQL Server數(shù)據(jù)庫設(shè)置自動備份策略的完整步驟
這篇文章主要給大家介紹了關(guān)于SQL Server數(shù)據(jù)庫設(shè)置自動備份策略的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用sql server具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-09-09
SQL Server 不刪除信息重新恢復(fù)自動編號列的序號的方法
SQL Server 不刪除信息重新恢復(fù)自動編號列的序號的方法...2007-11-11
在安裝了Sql2000的基礎(chǔ)上安裝Sql2005的詳細(xì)過程 圖文
在安裝了Sql2000的基礎(chǔ)上安裝Sql2005的詳細(xì)過程 圖文方法,需要的朋友可以參考下。2011-03-03
用SQL語句實現(xiàn)隨機(jī)查詢數(shù)據(jù)并不顯示錯誤數(shù)據(jù)的方法
用SQL語句實現(xiàn)隨機(jī)查詢數(shù)據(jù)并不顯示錯誤數(shù)據(jù)的方法...2007-11-11

