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进行说明:
 

 
  
  1. declare @XMLVar xml = '  
  2. <catalog>  
  3.  <book category="ITPro">  
  4.  <title>Windows Step By Step</title>  
  5.  <author>Bill Zack</author>  
  6.  <price>49.99</price>  
  7.  </book>  
  8.  <book category="Developer">  
  9.  <title>Developing ADO .NET</title>  
  10.  <author>Andrew Brust</author>  
  11.  <price>39.93</price>  
  12.  </book>  
  13.  <book category="ITPro">  
  14.  <title>Windows Cluster Server</title>  
  15.  <author>Stephen Forte</author>  
  16.  <price>59.99</price>  
  17.  </book>  
  18. </catalog>  

1.XML.Modify(Insert)语句介绍

A.利用as first,at last,before,after四个参数将元素插入指定的位置
 

 
  
  1.  
  2. set @XMLVar.modify(  
  3.  'insert <first name="at first" /> as first into (/catalog[1]/book[1])')  
  4. set @XMLVar.modify(  
  5.  'insert <last name="at last"/> as last into (/catalog[1]/book[1])')  
  6. set @XMLVar.modify(  
  7.  'insert <before name="before"/> before (/catalog[1]/book[1]/author[1])')  
  8. set @XMLVar.modify(  
  9.  'insert <after name="after"/> after (/catalog[1]/book[1]/author[1])')  
  10. SELECT @XMLVar.query('/catalog[1]/book[1]');  
  11.    

结果集为:

 1: <book category="ITPro">
 2: <first name="at first" />
 3: <title>Windows Step By Step</title>
 4: <before name="before" />
 5: <author>Bill Zack</author>
 6: <after name="after" />
 7: <price>49.99</price>
 8: <last name="at last" />
 9: </book>
 

B.将多个元素插入文档中

 

 
  
  1. --方法一:利用变量进行插入  
  2. DECLARE @newFeatures xml;  
  3. SET @newFeatures = N';  
  4. <first>one element</first>  
  5. <second>second element</second>'  
  6. SET @XMLVar.modify(' )  
  7. insert sql:variable("@newFeatures")  
  8. into (/catalog[1]/book[1])'  
  9. --方法二:直接插入  
  10. set @XMLVar.modify(')  
  11. insert (<first>one element</first>,<second>second element</second>)  
  12. into (/catalog[1]/book[1]/author[1])'  
  13. SELECT @XMLVar.query('/catalog[1]/book[1]');  
  14.   

结果集为:
 
 1: <book category="ITPro">
 2:  <title>Windows Step By Step</title>
 3:  <author>Bill Zack
 4:  <first>one element</first>
 5:  <second>second element</second>
 6:  </author>
 7:  <price>49.99</price>
 8:  <first>one element</first>
 9:  <second>second element</second>
 10: </book>
 
 

C.将属性插入文档中
 

 
  
  1. --使用变量插入  
  2. declare @var nvarchar(10) = '变量插入' 
  3. set @XMLVar.modify(  
  4. 'insert (attribute var {sql:variable("@var")}))  
  5. into (/catalog[1]/book[1])'  
  6. --直接插入  
  7. set @XMLVar.modify(  
  8. 'insert (attribute name {"直接插入"}))  
  9. into (/catalog[1]/book[1]/title[1])'  
  10. --多值插入  
  11. set @XMLVar.modify(  
  12. 'insert (attribute Id {"多值插入1"},attribute name {"多值插入2"}) )  
  13. into (/catalog[1]/book[1]/author[1])'  
  14. SELECT @XMLVar.query('/catalog[1]/book[1]');  

结果集为:

 1: <book category="ITPro" var="变量插入">
 2: <title name="直接插入">Windows Step By Step</title>
 3: <author Id="多值插入1" name="多值插入2">Bill Zack</author>
 4: <price>49.99</price>
 5: </book>
 

D.插入文本节点
 

 
  
  1. set @XMLVar.modify(  
  2. 'insert text{"at first"as first)  
  3. into (/catalog[1]/book[1])'  
  4. SELECT @XMLVar.query('/catalog[1]/book[1]');  

结果集为:

 1: <book category="ITPro">
 2: at first
 3:  <title>Windows Step By Step</title>
 4:  <author>Bill Zack</author>
 5:  <price>49.99</price>
 6: </book>
 

注意:插入本文同样具体 as first,as last,before,after四种选项,可以参考A中的使用方法

E.插入注释节点
 

 
  
  1. set @XMLVar.modify(  
  2. 'insert <!--插入评论-->)  
  3. before (/catalog[1]/book[1]/title[1])'  
  4. SELECT @XMLVar.query('/catalog[1]/book[1]');  

结果集为:

 1: <book category="ITPro">
 2: <!--插入评论-->
 3: <title>Windows Step By Step</title>
 4: <author>Bill Zack</author>
 5: <price>49.99</price>
 6: </book>
注意插入注释节点同样具体 as first,as last,before,after四种选项,可以参考A中的使用方法
 
 F.插入处理指令
 

 
  
  1. set @XMLVar.modify(  
  2. 'insert <?Program "Instructions.exe" ?>)  
  3. before (/catalog[1]/book[1]/title[1])'  
  4. SELECT @XMLVar.query('/catalog[1]/book[1]');结果集为:  
  5.  1: <book category="ITPro">  
  6.  2:  <?Program "Instructions.exe" ?>  
  7.  3:  <title>Windows Step By Step</title>  
  8.  4:  <author>Bill Zack</author>  
  9.  5:  <price>49.99</price>  
  10.  6: </book> 

注意插入处理指令同样具体 as first,as last,before,after四种选项,可以参考A中的使用方法
  

G.根据 if 条件语句进行插入
 

 
  
  1. set @XMLVar.modify(  
  2. 'insert )  
  3. if (/catalog[1]/book[1]/title[2]) then 
  4.  text{"this is a 1 step"}  
  5. else ( text{"this is a 2 step"} )  
  6.  into (/catalog[1]/book[1]/price[1])'  
  7. SELECT @XMLVar.query('/catalog[1]/book[1]');  

结果集为:
 1: <book category="ITPro">
 2: <title>Windows Step By Step</title>
 3: <author>Bill Zack</author>
 4: <price>49.99this is a 2 step</price>
 5: </book>
 
2.XML.Modify(delete)语句介绍
 

 
  
  1. --删除属性  
  2. set @XMLVar.modify('delete /catalog[1]/book[1]/@category')  
  3. --删除节点  
  4. set @XMLVar.modify('delete /catalog[1]/book[1]/title[1]')  
  5. --删除内容  
  6. set @XMLVar.modify('delete /catalog[1]/book[1]/author[1]/text()')  
  7. --全部删除  
  8. set @XMLVar.modify('delete /catalog[1]/book[2]')  
  9.    
  10. SELECT @XMLVar.query('/catalog[1]');  

结果集为:

 1: <catalog>
 2: <book>
 3: <author />
 4: <price>49.99</price>
 5: </book>
 6: <book category="ITPro">
 7: <title>Windows Cluster Server</title>
 8: <author>Stephen Forte</author>
 9: <price>59.99</price>
 10: </book>
 11: </catalog>
 
 

3.XML.Modify(replace)语句介绍
 

 
  
  1. --替换属性  
  2. set @XMLVar.modify('replace value of(/catalog[1]/book[1]/@category))  
  3.  with ("替换属性")'  
  4. --替换内容  
  5. set @XMLVar.modify('replace value of(/catalog[1]/book[1]/author[1]/text()[1]))  
  6.  with("替换内容")'  
  7. --条件替换  
  8. set @XMLVar.modify('replace value of (/catalog[1]/book[2]/@category))  
  9. with(  
  10. if(count(/catalog[1]/book)>4) then 
  11.  "条件替换1" 
  12. else 
  13.  "条件替换2")'  
  14.    
  15. SELECT @XMLVar.query('/catalog[1]');  

结果集为:
 1: <catalog>
 2:  <book category="替换属性">
 3:  <title>Windows Step By Step</title>
 4:  <author>替换内容</author>
 5:  <price>49.99</price>
 6:  </book>
 7:  <book category="条件替换2">
 8:  <title>Developing ADO .NET</title>
 9:  <author>Andrew Brust</author>
 10:  <price>39.93</price>
 11:  </book>
 12:  <book category="ITPro">
 13:  <title>Windows Cluster Server</title>
 14:  <author>Stephen Forte</author>
 15:  <price>59.99</price>
 16:  </book>
 17: </catalog>