t-sql中的xml操作在我們平時做項目的過程中用的很少,因為我們處理的數據量很少,除非一些用到xml的地方,t-sql中xml操作一般用在數據量很大,性能優化的地方,當然我在平時做項目的時候也是沒用過,但是學一點,以備不時之需。
今天就講一下t-sql中簡單的xml操作語法。
一,簡單的xml操作
1,我們先建一個表: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,添加學生節點,就是添加一個學生,用到modify的insert into語句,后面的/為xml節點的路徑。
示例代碼:
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,刪除學生節點,用到modify的delete語句,[@id="1003"]為刪除的條件,像t-sql中的where一樣。
示例代碼:
update dbo.Student set content.modify(' delete /Students/Student[@id="1003"] ')
6,更改學生節點字段,用到modify語句中的replace語句,text()表示的是add節點的值。
示例代碼:
update dbo.Student set content.modify(' replace value of (/Students/Student[@id="1003"]/add/text())[1] with "江蘇徐州" ')
7,更改學生節點屬性,用到modify語句中的replace語句,@id表示的是add節點的屬性的值。
示例代碼:
update dbo.Student set content.modify(' replace value of (/Students/Student[@id="1003"]/@id)[1] with 1004 ')
8,查詢所有學生的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操作實例
上面說的都是xml一些簡單的操作,下面我們結合t-sql中的xml操作,存儲過程和事務做一個實例,以便我們更好的去理解,運用。
實例要求:定義一個存儲過程,要求傳遞一個xml變量類型,將xml內的指定的ID記錄,從Table1全部掉,刪除操作要求利用事務;
1,首先我們需要建一張表,然后插一些數據。
示例代碼:
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,實例要求我們需要建一個存儲過程,然后傳遞一個xml變量,然后將xml內的指定的ID記錄,從Table1全部掉,而且刪除操作用事務。
我們存儲過程就得將xml進行解析得到xml中的ID記錄,這個操作我們就得用到游標,游標我會在以后的做講解,游標遍歷得到的ID記錄,
查詢Table1表中是否存在,如果存在記錄下來,並用事務去刪除。
示例代碼:
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簡單用法,有錯誤的地方希望園友指正。
以后還會整理一些編程的知識分享給大家,希望大家多多關注。。。
