SQL SERVER XML学习总结(三)

2014-11-24 10:20:45 · 作者: · 浏览: 2
使用 declare @xml xml ='' declare @pos int=2 select @xml.value('local-name((/root/info/@*[position()=sql:variable("@pos")])[1])','nvarchar(20)') --sql:column:将普通数据列和Xml数据合并 declare @tb table (id int,data xml) insert into @tb(id,data) select 1,'BenbenMG' select id,data=data.query('{sql:column("id")}{/root/info/name}{/root/info/type}') from @tb ---contains:模糊查询contains(.,'XX') select t.c.query('.') from @dataSource.nodes('/root/info/user[./name[contains(.,"笨")]]') t(c) 5.5 Modify() 5.5.1 Insert --在某一节点下添加一个子节点insert into,as first/as last 指定节点插入的位置 set @dataSource.modify('insert F as last into (/root/info/user)[1]') select @dataSource --添加某一节点的同级节点,before/after 添加同级节点 set @dataSource.modify('insert A1 before (/root/info/user/name)[1]') select @dataSource --插入属性 declare @a int=111 set @dataSource.modify('insert (attribute a {sql:variable("@a")}, attribute b {".3"}) into (/root/info/user[@uid=001])[1]') select @dataSource 5.5.2 Delete --删除属性 set @dataSource.modify('delete /root/info/user/@uid') select @dataSource --删除节点 set @dataSource.modify('delete /root/info/user/type') select @dataSource --删除节点内容 set @dataSource.modify('delete /root/info/user/type/text()') select @dataSource --删除所有属性为空的节点 set @dataSource.modify('delete //*[empty(./*)]') select @dataSource 5.5.3 Replace --修改节点值 set @dataSource.modify('replace value of (/root/info/user/name/text())[1] with "小笨笨"') select @dataSource --修改属性值 set @dataSource.modify('replace value of (/root/info/user/@uid)[1] with "0001"') select @dataSource