• [织梦吧]唯一域名:www.upgvv.com,织梦DedeCMS学习平台.

当前位置: > 编程与数据库 > mssql2005教程 >

SQL Server中的XML数据进行insert、update、delete操作实现代码

来源: www.upgvv.com 编辑:织梦吧 时间:2012-07-01点击:

SQL Server 2005/2008增加了对XML数据的支持,同时也新增了几种操作XML的方法,本文主要以SQL Server 2008为例介绍如何对XML数据进行insert、update、deleteSQL

Server中新增加了XML.Modify()方法,分别为xml.modify(insert),xml.modify(delete),xml.modify(replace)对应XML的插入,删除和修改操作。 
本文以下面XML为例,对三种DML进行说明: 

  1. declare @XMLVar XML;  
  2. SET @XMLVar= '  
  3.  
  4. <catalog>  
  5. <book category="ITPro">  
  6. <title>Windows Step By Step</title>  
  7. <author>Bill Zack</author>  
  8. <price>49.99</price>  
  9. </book>  
  10. <book category="Developer">  
  11. <title>Developing ADO .NET</title>  
  12. <author>Andrew Brust</author>  
  13. <price>39.93</price>  
  14. </book>  
  15. <book category="ITPro">  
  16. <title>Windows Cluster Server</title>  
  17. <author>Stephen Forte</author>  
  18. <price>59.99</price>  
  19. </book>  
  20. </catalog>  
1.XML.Modify(Insert)语句介绍 
A.利用as first,at last,before,after四个参数将元素插入指定的位置 
  1. set @XMLVar.modify(  
  2. 'insert <first name="at first" /> as first into (/catalog[1]/book[1])')  
  3.  
  4.  
  5. set @XMLVar.modify(  
  6. 'insert <last name="at last"/> as last into (/catalog[1]/book[1])')  
  7.  
  8.  
  9. set @XMLVar.modify(  
  10. 'insert <before name="before"/> before (/catalog[1]/book[1]/author[1])')  
  11.  
  12. set @XMLVar.modify(  
  13. 'insert <after name="after"/> after (/catalog[1]/book[1]/author[1])')  
  14. SELECT @XMLVar.query('/catalog[1]/book[1]');  

结果集为: 

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

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

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

结果集为: 

  1. 1: <book category="ITPro">  
  2. 2: <title>Windows Step By Step</title>  
  3. 3: <author>Bill Zack  
  4. 4: <first>one element</first>  
  5. 5: <second>second element</second>  
  6. 6: </author>  
  7. 7: <price>49.99</price>  
  8. 8: <first>one element</first>  
  9. 9: <second>second element</second>  
  10. 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.  
  8. --直接插入  
  9. set @XMLVar.modify(  
  10. 'insert (attribute name {"直接插入"}))  
  11. into (/catalog[1]/book[1]/title[1])'  
  12.  
  13.  
  14. --多值插入  
  15. set @XMLVar.modify(  
  16. 'insert (attribute Id {"多值插入1"},attribute name {"多值插入2"}) )  
  17. into (/catalog[1]/book[1]/author[1])'  
  18. SELECT @XMLVar.query('/catalog[1]/book[1]');  

结果集为:

  1. 1: <book category="ITPro" var="变量插入">  
  2. 2: <title name="直接插入">Windows Step By Step</title>  
  3. 3: <author Id="多值插入1" name="多值插入2">Bill Zack</author>  
  4. 4: <price>49.99</price>  
  5. 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. 1: <book category="ITPro">  
  2. 2: at first  
  3. 3: <title>Windows Step By Step</title>  
  4. 4: <author>Bill Zack</author>  
  5. 5: <price>49.99</price>  
  6. 6: </book>  

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

  1. set @XMLVar.modify(  
  2. N'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]');  

结果集为: 
1: <book category="ITPro"> 
2: <?Program "Instructions.exe" ?> 
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中的使用方法 

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.  
  5. --删除节点  
  6. set @XMLVar.modify('delete /catalog[1]/book[1]/title[1]')  
  7.  
  8.  
  9. --删除内容  
  10. set @XMLVar.modify('delete /catalog[1]/book[1]/author[1]/text()')  
  11.  
  12.  
  13. --全部删除  
  14. set @XMLVar.modify('delete /catalog[1]/book[2]')  
  15.  
  16. SELECT @XMLVar.query('/catalog[1]');  

结果集为: 

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

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

  1. --替换属性  
  2. set @XMLVar.modify(N'replace value of(/catalog[1]/book[1]/@category)  
  3. with ("替换属性")' )  
  4. --替换内容  
  5. set @XMLVar.modify(N'replace value of(/catalog[1]/book[1]/author[1]/text()[1])  
  6. with("替换内容")' )  
  7. --条件替换  
  8. set @XMLVar.modify(N'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]');  
  16. [code]  
  17. 结果集为:  
  18. [code]  
  19. 1: <catalog>  
  20. 2: <book category="替换属性">  
  21. 3: <title>Windows Step By Step</title>  
  22. 4: <author>替换内容</author>  
  23. 5: <price>49.99</price>  
  24. 6: </book>  
  25. 7: <book category="条件替换2">  
  26. 8: <title>Developing ADO .NET</title>  
  27. 9: <author>Andrew Brust</author>  
  28. 10: <price>39.93</price>  
  29. 11: </book>  
  30. 12: <book category="ITPro">  
  31. 13: <title>Windows Cluster Server</title>  
  32. 14: <author>Stephen Forte</author>  
  33. 15: <price>59.99</price>  
  34. 16: </book>  
  35. 17: </catalog>  
猜你也喜欢看这些...