t-sql中的xml操作在我們平時(shí)做項(xiàng)目的過程中用的很少,因?yàn)槲覀兲幚淼臄?shù)據(jù)量很少,除非一些用到xml的地方,t-sql中xml操作一般用在數(shù)據(jù)量很大,性能優(yōu)化的地方,當(dāng)然我在平時(shí)做項(xiàng)目的時(shí)候也是沒用過,但是學(xué)一點(diǎn),以備不時(shí)之需。
今天就講一下t-sql中簡單的xml操作語法。
一,簡單的xml操作
1,我們先建一個(gè)表 : Student(id,content /xml)
示例代碼:
create table Student (id int primary key ,content xml) insert into dbo.Student values ( 1000 , ' <Students> <Student id="1001"> <name>aaa</name> <age>20</age> <birthday>1991-2-20</birthday> </Student> <Student id="1002"> <name>bbb</name> <age>21</age> <birthday>1990-2-20</birthday> </Student> </Students> ' )
2,添加學(xué)生節(jié)點(diǎn),就是添加一個(gè)學(xué)生,用到modify的insert into語句,后面的/為xml節(jié)點(diǎn)的路徑。
示例代碼:
update dbo.Student set content.modify( ' insert <Student id="1003"> <name>aaa</name> <age>20</age> <birthday>1991-2-20</birthday> </Student> as last into (/Students)[1] ' )
3,添加屬性,用到modify的insert into語句。
示例代碼:
update dbo.Student set content.modify( ' insert attribute sex {"男"} into (/Students/Student[@id="1003"])[1] ' )
4,添加字段add ,用到modify的insert into語句。
示例代碼:
update dbo.Student set content.modify( ' insert <add>江蘇豐縣</add> as last into (/Students/Student[@id="1003"])[1] ' )
5,刪除學(xué)生節(jié)點(diǎn),用到modify的delete語句,[@id="1003"]為刪除的條件,像t-sql中的where一樣。
示例代碼:
update dbo.Student set content.modify( ' delete /Students/Student[@id="1003"] ' )
6,更改學(xué)生節(jié)點(diǎn)字段,用到modify語句中的replace語句,text()表示的是add節(jié)點(diǎn)的值。
示例代碼:
update dbo.Student set content.modify( ' replace value of (/Students/Student[@id="1003"]/add/text())[1] with "江蘇徐州" ' )
7,更改學(xué)生節(jié)點(diǎn)屬性,用到modify語句中的replace語句,@id 表示的是add節(jié)點(diǎn)的屬性的值。
示例代碼:
update dbo.Student set content.modify( ' replace value of (/Students/Student[@id="1003"]/@id)[1] with 1004 ' )
8,查詢所有學(xué)生的ID和姓名。
示例代碼:
select Student1.content.value( ' ./@id ' , ' int ' ) as ID, Student1.content.value( ' (/Students/Student/name)[1] ' , ' nvarchar(30) ' ) as StuName from dbo.Student CROSS APPLY content.nodes( ' /Students/Student ' ) as Student1(content)
二,xml操作實(shí)例
上面說的都是xml一些簡單的操作,下面我們結(jié)合t-sql中的xml操作,存儲(chǔ)過程和事務(wù)做一個(gè)實(shí)例,以便我們更好的去理解,運(yùn)用。
實(shí)例要求:定義一個(gè)存儲(chǔ)過程,要求傳遞一個(gè)xml變量類型,將xml內(nèi)的指定的ID記錄,從Table1全部掉,刪除操作要求利用事務(wù);
1,首先我們需要建一張表,然后插一些數(shù)據(jù)。
示例代碼:
create table Table1 ( ID int primary key , Name nvarchar ( 50 ) not null ) insert into dbo.Table1 values ( 1 , ' Name1 ' ),( 2 , ' Name2 ' ),( 3 , ' Name3 ' ) select * from Table1
2,實(shí)例要求我們需要建一個(gè)存儲(chǔ)過程,然后傳遞一個(gè)xml變量,然后將xm l內(nèi)的指定的ID記錄,從Table1全部掉,而且刪除操作用事務(wù)。
我們存儲(chǔ)過程就得將xml進(jìn)行解析得到xml中的ID記錄,這個(gè)操作我們就得用到游標(biāo),游標(biāo)我會(huì)在以后的做講解,游標(biāo)遍歷得到的ID記錄,
查詢 Table1 表中是否存在,如果存在記錄下來,并用事務(wù)去刪除。
示例代碼:
create proc proc_Table1 ( @ID xml ) as begin declare @Temp table (ID1 int ) insert into @Temp (ID1) select ParamValues123.ID2.value( ' ./@id ' , ' int ' ) as asdfasdf FROM @ID .nodes( ' /nodes/node ' ) as ParamValues123(ID2) begin transaction t1 declare @j int ; select @j = count (ID1) from @Temp ; declare curs_Table1 cursor for select ID1 from @Temp ; declare @ID2 int ; declare @i int ; set @i = 0 ; open curs_Table1; fetch next from curs_Table1 into @ID2 ; while @@FETCH_STATUS = 0 begin if ( exists ( select ID from dbo.Table1 where ID = @ID2 )) set @i = @i + 1 ; fetch next from curs_Table1 into @ID2 ; end close curs_Table1; deallocate curs_Table1; if @i = @j begin delete from dbo.Table1 Where ID in ( SELECT ParamValues123.ID2.value( ' ./@id ' , ' int ' ) as ID FROM @ID .nodes( ' /nodes/node ' ) as ParamValues123(ID2) ) commit transaction t1; end else rollback transaction t1; -- drop table @Temp; -- select * from Table1 Where ID in -- ( -- SELECT ParamValues123.ID2.value('./@id','int') as asdfasdf -- FROM @ID.nodes('/nodes/node') as ParamValues123(ID2) -- ) end
以上是t-sql中的xml簡單用法,有錯(cuò)誤的地方希望園友指正。
以后還會(huì)整理一些編程的知識(shí)分享給大家,希望大家多多關(guān)注。。。
?
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長非常感激您!手機(jī)微信長按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】元
