淺談SQL Server 2005 XQuery新功能(2)

上一篇 / 下一篇  2009-11-27 11:27:27 / 个人分类:工作中需要的知识

3.3.5內容函數(Context Functions):此類函數在XQuery陳述式所指出的節點中依指定的位置取得資料,其函數說明如表3-5所示。

3-5 SQL Server 2005XQuery內容函數

函數名稱

說明

last()

傳回目前所處理序列中的項目號碼。具體而言,它會傳回序列中最後一個項目的整數索引。序列中第一個項目的索引值為1

position()

傳回整數值,以指出目前所處理的項目序列中內容項目的位置。

其用法如下:

Example:

declare@xxml

set@x='

<gun bore="9mm"><item>M92F</item>

<item>G-19</item>

<item>MP5</item>

</gun>

<gun bore="5.56"><item>M16</item>

<item>G36C</item>

<item>M4A1</item>

<item>M249</item>

</gun>'

select@x.query('for $i in /gun[last() ]

return

$i/*[position() <=2]')

Result:

<item>M16</item>

<item>G36C</item>

 

3.3.6序列的相關函數(Functions on Sequences):此類函數在處理XQuery陳述式所指出的節點的資料序列,可做空序列檢查、排除重覆資料等運算,其函數說明如表3-6所示。

3-6 SQL Server 2005XQuery序列的相關函數

函數名稱

說明

empty($argas item() *)

如果$arg的值是空序列,則傳回True。否則,此函數會傳回False

distinct-values($argas xdt:anyAtomicType*)

移除$arg所指定的時序中的重複值。如果$arg是空白時序,則函數會傳回空白時序。

id($arg as xs:IDREF*)

傳回含有xs:ID值的元素節點序列,這些值符合$arg所提供的一或多個xs:IDREF值。

其用法如下:

Example:

declare@xxml

set@x='

<gun><item>M16</item>

<item>G36C</item>

<item>G36C</item>

<item>M4A1</item>

<item>M4A1</item>

<item>M249</item>

</gun>

<gun bore="7.62"><item>T74</item>

<item>AK47</item>

</gun>'

select@x.query('

for $i in distinct-values(/gun[empty(@bore) ]/item)

return <item>{$i}</item>')

Result:

<item>M16</item>

<item>G36C</item>

<item>M4A1</item>

<item>M249</item>

 

3.3.7彙總函數(Aggregate Functions):針對XQuery中指定的節點的資料,求取其總數,對於數值型資料可以計算其平均、最大值、最小值以及加總等彙總運算,其函數說明如表3-7所示。

3-7 SQL Server 2005XQuery彙總函數

函數名稱

說明

count($argas item() *)

傳回$arg指定時序中所包含的項目號碼。

avg($argas xdt:anyAtomicType*)

傳回數字序列的平均值。

min($argas xdt:anyAtomicType*)

從不可部份完成值$arg的序列傳回,一個值的項目少於所有其他的值。

max($arg as xdt:anyAtomicType*)

從不可部份完成值的序列$arg中,傳回其值大於所有其他項目之值的項目。

sum($arg as xdt:anyAtomicType*)

傳回數字序列的總和。

其用法如下:

Example:

declare@xxml

set@x='<root>

<item value="1">a</item>

<item value="3">b</item>

<item value="2">c</item>

<item value="5">d</item>

<item value="4">e</item>

</root>'

select@x.query('

for $i in /root

return

<s>count: {count($i/item) };

avg:{avg($i/item/@value[1]) };

min:{min($i/item/@value[1]) };

max:{max($i/item/@value[1]) };

sum:{sum($i/item/@value[1]) }</s>

')

Result:

<s>count: 5;

avg:3;

min:1;

max:5;

sum:15</s>

 

3.3.8建構函式函數(Constructor Functions):可以讓使用者自行訂義XSD類型,其函數說明如表3-8所示。

3-8 SQL Server 2005XQuery建構函式函數

函數名稱

說明

TYP($atomicvalueas xdt:anyAtomicType)

建構函式函數可由指定輸入,建立任何XSD內建或使用者自訂的不可部份完成類型。

 

3.3.9 Data Accessor函數(Data Accessor Functions):取得XQuery所指出的節點資料,其函數說明如表3-9所示。

3-9 SQL Server 2005XQuery Data Accessor函數

函數名稱

說明

string($argas item())

傳回以字串表示的$arg值。

data ($argas item() *)

傳回$arg指定的每一個項目的具類型值。

使用方式如下所示:

Example:

declare@xxml

set@x='<?xml version="1.0" encoding="UTF-8" ?>

<!--  This is a comment -->

<root>

  <a>10</a>

just text

  <b attr="x">20</b>

</root>

'

select@x.query('string(/) ')

select@x.query('data(/) ')

Result:

--------------------------------------------

This is a comment 10

just text

  20

(1 row(s) affected)

--------------------------------------------

10

just text

  20

(1 row(s) affected)

 

3.3.10布林建構函式(Boolean Constructor Functions):產生布林值,其函數說明如表3-10所示。

3-10 SQL Server 2005XQuery布林建構函式

函數名稱

說明

true()

傳回xs:booleanTrue。這相當於xs:boolean("1")

False()

傳回xs:booleanFalse。這相當於xs:boolean("0")

使用方式如下所示:

Example:

DECLARE@xXML

SET@x='<ROOT><elem attr="aaa">bbb</elem></ROOT>'

select@x.value('

if ((/ROOT/elem/@attr) [1] eq "aaa") then

fn:true()

else fn:false() ','bit')

Result:

1

 

3.3.11 SQL Server XQuery擴充函數:使用sql:column()sql:variable() XQuery擴充程式函數,在XML內繫結非XML關聯式資料。column()用以指定針對儲存在xml類型的變數或資料行中之XML執行個體的查詢,當查詢使用另一個非XML資料行的值,以同時查詢關聯式資料與XML資料。variable()可以在XQuery陳述式中用使用Transact-SQL變數或參數中的值,以將關聯式資料及XML資料合併在一起。其函數說明如表3-11所示。

3-11 SQL Server 2005XQuery擴充函數

函數名稱

說明

column("columnName")

讀取同一筆資料中其他非xml資料類型的欄位資料。

variable("variableName")

XML資料執行個體中公開含有SQL關聯式值的變數。

使用方式如下所示:

Example:

declare@xxml

declare@ivarchar(10)

set@i='b'

set@x='<root>

<item value="1">a</item>

<item value="3">b</item>

<item value="2">c</item>

<item value="3">d</item>

<item value="4">e</item>

</root>'

select'4'asv,@xasxinto#t

select@x.query('

for $i in /root/item

where data($i) =sql:variable("@i")

or $i/@value=sql:column("v")

return

$i')

from#t

Result:

<itemvalue="3">b</item>

<itemvalue="4">e</item>

 4.後記

  很可惜的,SQL Server 2005不支援XQueryLet語法,也無法像在使用Table Join一樣可以直接整合兩個XML文件的資料,雖然可以使用sql:columnsql:variable來引用其他欄位與變數,卻也有種種限制。但種而言之,可以將XML訂為資料型態並引用XQuery來查詢,在關聯式資料庫系統對於XML的支援,已經可以說是跨出一大步了,讓使用者不需要在定義複雜的表格或繕寫剖析程式、甚至另外建置原生型XML資料庫系統,便可以在原有的關聯式資料庫系統架構中整合XML的應用。資訊技術的演進是如此快速,不久的將來各家關聯式資料庫系統會對XML有更完善的整合與支援。 

參考文獻

[1] Don Chamberlin, Jonathan Robie, and Daniela Florescu, "Quilt: An XML Query Language for Heterogeneous Data Sources,”Proceedings of WebDB 2000 Conference, in Lecture Notes in Computer Science, Springer-Verlag, 2000.

[2] SQL Server 2005 Books Online

TAG: 2005 server Server SQL XML xml xquery XQuery

 

评分:0

我来说两句

日历

« 2024-03-22  
     12
3456789
10111213141516
17181920212223
24252627282930
31      

数据统计

  • 访问量: 17570
  • 日志数: 16
  • 图片数: 1
  • 建立时间: 2008-09-14
  • 更新时间: 2010-07-21

RSS订阅

Open Toolbar