sqlserver中对xml进行操作
1 --1、创建xml测试数据库表Xml_Table 2 use Fly_Test --测试数据库 3 go 4 5 create table Xml_Table(ID INT identity PRIMARY KEY, XmlData XML); 6 --2、插入测试数据 7 insert into Xml_Table(XmlData) values 8 (‘<book id="0001"> 9 <title>SqlServer2005</title> 10 <author>Fly</author> 11 <price>21</price> 12 </book> 13 ‘); 14 insert into Xml_Table(XmlData) values 15 (‘<book id="0002"> 16 <title>SqlServer2008</title> 17 <author>Fly</author> 18 <price>22</price> 19 </book> 20 ‘); 21 insert into Xml_Table(XmlData) values 22 (‘<book id="0003"> 23 <title>SqlServer2012</title> 24 <author>Fly</author> 25 <price>23</price> 26 </book> 27 ‘); 28 --3、查询 29 select * from Xml_Table; 30 --4、对XML操作真正开始了 31 --SQLServer2005 中对 XML 的处理功能显然增强了很多,提供了 query(),value(),exist(),modify(),nodes() 32 --查询所有书的名称及作者 33 select XmlData.query(‘/book‘) as Title,XmlData.query(‘/book/author‘) as Author from Xml_Table; 34 --显然这不是我们想要的数据 35 select XmlData.value(‘(/book/title)[1]‘,‘nvarchar(max)‘) as Title, 36 XmlData.value(‘(/book/author)[1]‘,‘nvarchar(max)‘) as Author from Xml_Table; 37 --查询数目编号为0001的书的信息 38 select XmlData.value(‘(/book/title)[1]‘,‘nvarchar(max)‘) as Title, 39 XmlData.value(‘(/book/@id)[1]‘,‘nvarchar(max)‘) as BookID from Xml_Table 40 where XmlData.value(‘(/book/@id)[1]‘,‘nvarchar(max)‘) = ‘0001‘; 41 --修改数目编号为0001 的价格为 11 42 update Xml_Table 43 set XmlData.modify(‘replace value of (/book[@id="0001"]/price/text())[1] with "11"‘); 44 --修改 所有的数目作者为Fly_12300 45 update Xml_Table 46 set XmlData.modify(‘replace value of (/book/author/text())[1] with "Fly_12300"‘) 47 --查看是否编号为0001的价格修改为11,且所有作者修改为Fly_12300 48 select XmlData.value(‘(/book/price)[1]‘,‘nvarchar(max)‘) as Title, 49 XmlData.value(‘(/book/@id)[1]‘,‘nvarchar(max)‘) as BookID, 50 XmlData.value(‘(/book/author)[1]‘,‘nvarchar(max)‘) as Author from Xml_Table 51 where XmlData.value(‘(/book/@id)[1]‘,‘nvarchar(max)‘) = ‘0001‘; 52 --添加属性 53 update Xml_Table 54 set XmlData.modify(‘insert attribute isbn {"12300321"} into (/book)[1]‘); 55 --查看是否存在属性isbn 56 select XmlData.value(‘(/book/@isbn)[1]‘,‘nvarchar(max)‘) as isbn, 57 XmlData.value(‘(/book/@id)[1]‘,‘nvarchar(max)‘) as BookID from Xml_Table 58 where XmlData.value(‘(/book/@id)[1]‘,‘nvarchar(max)‘) = ‘0001‘; 59 --在编号为0001的添加子节点 category 为 Computer 的分类 60 update Xml_Table 61 set XmlData.modify(‘insert <category>Computer</category> before (/book[@id=0001]/author)[1]‘); 62 --查看是否添加了category节点 63 select XmlData.value(‘(/book/category)[1]‘,‘nvarchar(max)‘) as category, 64 XmlData.value(‘(/book/@id)[1]‘,‘nvarchar(max)‘) as BookID,XmlData from Xml_Table 65 where XmlData.value(‘(/book/@id)[1]‘,‘nvarchar(max)‘) = ‘0001‘; 66 --删除节点 67 update Xml_Table 68 set XmlData.modify(‘delete /book[@id=0001]/category‘); 69 --查看是否删除了category节点 70 select XmlData.value(‘(/book/category)[1]‘,‘nvarchar(max)‘) as category, 71 XmlData.value(‘(/book/@id)[1]‘,‘nvarchar(max)‘) as BookID,XmlData from Xml_Table 72 where XmlData.value(‘(/book/@id)[1]‘,‘nvarchar(max)‘) = ‘0001‘; 73 --nodes() 查询 book的编码 74 select ids.value(‘@id‘, ‘varchar(max)‘),ids.value(‘(title)[1]‘,‘nvarchar(max)‘) title from Xml_Table 75 CROSS APPLY XmlData.nodes(‘//book‘) as X(ids) ; 76 --exist() 77 select XmlData.value(‘(/book/@id)[1]‘,‘nvarchar(max)‘) as BookID 78 from Xml_Table 79 where XmlData.exist(‘(/book/@id)‘)=1 --判断是否存在 80 81 82 --新建测试表 83 create table Books(ID nvarchar(32) not null,Name nvarchar(64)); 84 insert into Books values (‘0001‘,‘MSSQLServer2005‘); --书名MSSQLServer2005 85 insert into Books values (‘0002‘,‘MSSQLServer2008‘); --书名MSSQLServer2008 86 insert into Books values (‘0003‘,‘MSSQLServer2012‘); --书名MSSQLServer2012 87 --以下为xml path 88 SELECT ID,NAME FROM [dbo].[Books] FOR XML AUTO; 89 SELECT ID,NAME FROM [dbo].[Books] FOR XML AUTO ,ELEMENTS ,ROOT(‘books‘); 90 SELECT ID as ‘BookID‘,NAME as ‘BookName‘ FROM [dbo].[Books] FOR XML RAW; 91 SELECT ID,NAME FROM [dbo].[Books] FOR XML RAW(‘book‘) ,ELEMENTS ,ROOT(‘books‘); 92 SELECT ID,NAME FROM [dbo].[Books] FOR XML PATH(‘‘) ; 93 SELECT ID as ‘Detail/@ID‘,NAME as ‘Detail/Name‘ FROM [dbo].[Books] FOR XML PATH(‘Book‘), ROOT(‘Books‘); 94 SELECT STUFF((SELECT ‘;‘ + Name FROM [dbo].[Books] FOR XML PATH(‘‘)),1,1,‘‘); 95 96 --根据Books 表中的ID,Xml_Table 表中的XmlData ID属性 修改对应的 title属性 97 --即:根据在books中编码0001的 的名称 MSSQLServer2005 98 --修改为Xml_Table表中book编码为0001的title为 MSSQLServer2005 99 100 declare @data xml 101 declare @id nvarchar(36) 102 declare @name nvarchar(64) 103 declare custore_name cursor for 104 select Books.ID,Xml_Table.XmlData,Books.Name 105 from Books,Xml_Table 106 where Books.ID= Xml_Table.XmlData.value(‘(/book/@id)[1]‘,‘nvarchar(max)‘); 107 OPEN custore_name 108 FETCH NEXT FROM custore_name into @id, @data, @name 109 WHILE(@@FETCH_STATUS=0) 110 BEGIN 111 set @data.modify((‘replace value of (/book/title/text())[1] with sql:variable("@name")‘)) 112 update Xml_Table set XmlData = @data where XmlData.value(‘(/book/@id)[1]‘,‘nvarchar(max)‘) = @id 113 FETCH NEXT FROM custore_name into 114 @id, @data, @name 115 END 116 CLOSE custore_name 117 deallocate custore_name 118 119 select * from Xml_Table
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。