18個小例項入門SQLServer XML查詢

2021-08-26 15:40:21 字數 3724 閱讀 1729

/*

sql xml 入門:

--by erichu

--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='

everyday italian

giada de laurentiis

2005

30.00

harry potter

j k. rowling

2005

29.99

xquery kick start

james mcgovern

per bothner

kurt cagle

james linn

vaidyanathan nagarajan

2003

49.99

learning xml

erik t. ray

2003

39.95

'--測試語句,如果不理解語法請參考上面的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)

18個小例項入門SQLServer XML查詢

sql xml 入門 by erichu 1 xml 能認識元素 屬性和值 2 xpath 定址語言,類似windows目錄的查詢 沒用過dir命令的話就去面壁 語法格式,這些語法可以組合為條件 表示自己,表示父親,表示兒子,表示後代,name 表示按名字查詢,name 表示按屬性查詢 集合 條件 ...

PhantomJs入門小例項

phantomjs 入門小demo 為啥要用phantomjs呢,而不是superagent cheerio?後者只可以爬取一些靜態網頁是非常好用的,但是遇到了動態js載入資料的網頁就束手無策了。所以這裡選取了phantomjs作為爬蟲的工具 又稱無頭瀏覽器 配置phantomjs環境變數 編寫js...

vue router 簡單入門小例項

用vue.js vue router建立單頁應用,是非常簡單的。使用vue.js我們已經可以通過組合元件來組成應用程式,當需要把vue router新增進來,我們需要做 將元件 components 對映到路由 router 然後告訴vue router在 渲染它們。下面例子 於vue官方文件get...