SQL Server 2005/2008增加了對XML數據的支持,同時也新增了幾種操作XML的方法,本文主要以SQL Server 2008為例介紹如何對XML數據進行insert、update、delete
SQL Server中新增加了XML.Modify()方法,分別為xml.modify(insert),xml.modify(delete),xml.modify(replace)對應XML的插入,刪除和修改操作。
本文以下面XML為例,對三種DML進行說明:
代碼如下:
declare @XMLVar XML;
SET @XMLVar= ‘
1.XML.Modify(Insert)語句介紹
A.利用as first,at last,before,after四個參數將元素插入指定的位置
代碼如下:
set @XMLVar.modify(
‘insert
set @XMLVar.modify(
‘insert
set @XMLVar.modify(
‘insert
set @XMLVar.modify(
‘insert
SELECT @XMLVar.query(‘/catalog[1]/book[1]’);
結果集為:
代碼如下:
1:
2:
3:
4:
5:
6:
7:
9:
B.將多個元素插入文檔中
代碼如下:
–方法一:利用變量進行插入
DECLARE @newFeatures xml;
SET @newFeatures = N’;
SET @XMLVar.modify(‘ )
insert sql:variable(“@newFeatures”)
into (/catalog[1]/book[1])’
–方法二:直接插入
set @XMLVar.modify(‘)
insert (
into (/catalog[1]/book[1]/author[1])’
SELECT @XMLVar.query(‘/catalog[1]/book[1]’);
結果集為:
代碼如下:
1:
2:
3:
4:
5:
6:
7:
9:
10:
C.將屬性插入文檔中
代碼如下:
–使用變量插入
declare @var nvarchar(10) = ‘變量插入’
set @XMLVar.modify(
‘insert (Attribute var {sql:variable(“@var”)}))
into (/catalog[1]/book[1])’
–直接插入
set @XMLVar.modify(
‘insert (attribute name {“直接插入”}))
into (/catalog[1]/book[1]/title[1])’
–多值插入
set @XMLVar.modify(
‘insert (attribute Id {“多值插入1”},attribute name {“多值插入2”}) )
into (/catalog[1]/book[1]/author[1])’
SELECT @XMLVar.query(‘/catalog[1]/book[1]’);
結果集為:
代碼如下:
1:
2:
3:
4:
D.插入文本節點
代碼如下:
set @XMLVar.modify(
‘insert text{“at first”} as first)
into (/catalog[1]/book[1])’
SELECT @XMLVar.query(‘/catalog[1]/book[1]’);
結果集為:
代碼如下:
1:
2: at first
3:
4:
5:
注意:插入本文同樣具體 as first,as last,before,after四種選項,可以參考A中的使用方法
E.插入注釋節點
代碼如下:
set @XMLVar.modify(
N’insert
before (/catalog[1]/book[1]/title[1])’ )
SELECT @XMLVar.query(‘/catalog[1]/book[1]’);
結果集為:
1:
2:
3:
4:
5:
注意插入注釋節點同樣具體 as first,as last,before,after四種選項,可以參考A中的使用方法
F.插入處理指令
代碼如下:
set @XMLVar.modify(
‘insert
before (/catalog[1]/book[1]/title[1])’ )
SELECT @XMLVar.query(‘/catalog[1]/book[1]’);
結果集為:
1:
2:
3:
4:
5:
注意插入處理指令同樣具體 as first,as last,before,after四種選項,可以參考A中的使用方法
G.根據 if 條件語句進行插入
代碼如下:
set @XMLVar.modify(
‘insert
if (/catalog[1]/book[1]/title[2]) then
text{“this is a 1 step”}
else ( text{“this is a 2 step”} )
into (/catalog[1]/book[1]/price[1])’ )
SELECT @XMLVar.query(‘/catalog[1]/book[1]’);
結果集為:
1:
2:
3:
4:
2.XML.Modify(delete)語句介紹
代碼如下:
–刪除屬性
set @XMLVar.modify(‘delete /catalog[1]/book[1]/@category’)
–刪除節點
set @XMLVar.modify(‘delete /catalog[1]/book[1]/title[1]’)
–刪除內容
set @XMLVar.modify(‘delete /catalog[1]/book[1]/author[1]/text()’)
–全部刪除
set @XMLVar.modify(‘delete /catalog[1]/book[2]’)
SELECT @XMLVar.query(‘/catalog[1]’);
結果集為:
代碼如下:
1:
2:
3:
4:
6:
7:
8:
9:
11:
3.XML.Modify(replace)語句介紹
代碼如下:
–替換屬性
set @XMLVar.modify(N’replace value of(/catalog[1]/book[1]/@category)
with (“替換屬性”)’ )
–替換內容
set @XMLVar.modify(N’replace value of(/catalog[1]/book[1]/author[1]/text()[1])
with(“替換內容”)’ )
–條件替換
set @XMLVar.modify(N’replace value of (/catalog[1]/book[2]/@category)
with(
if(count(/catalog[1]/book)>4) then
“條件替換1”
else
“條件替換2”)’ )
SELECT @XMLVar.query(‘/catalog[1]’);
[code]
結果集為:
[code]
1:
2:
3:
4:
5:
7:
8:
9:
10:
12:
13:
14:
15:
17: