zoukankan      html  css  js  c++  java
  • SQL Server 中对XML数据的五种基本操作

    1.xml.exist
       输入为XQuery表达式,返回0,1或是Null。0表示不存在,1表示存在,Null表示输入为空
    2.xml.value
       输入为XQuery表达式,返回一个SQL Server标量值
    3.xml.query
       输入为XQuery表达式,返回一个SQL Server XML类型流
    4.xml.nodes
       输入为XQuery表达式,返回一个XML格式文档的一列行集
    5.xml.modify
       使用XQuery表达式对XML的节点进行insert , update 和 delete 操作。

    下面通过例子对上面的五种操作进行说明:

    declare @XMLVar xml = '

    <catalog> 

           <book category="ITPro">   

                  <title>Windows Step By Step</title>   

                  <author>Bill Zack</author>   

                  <price>49.99</price> 

           </book> 

           <book category="Developer">   

                  <title>Developing ADO .NET</title>   

                  <author>Andrew Brust</author>   

                  <price>39.93</price> 

           </book> 

           <book category="ITPro">   

                  <title>Windows Cluster Server</title>   

                  <author>Stephen Forte</author>   

                  <price>59.99</price> 

           </book>

    </catalog>'

    1. xml.exist

       select @XMLVar.exist('/catalog/book')-----返回1  

       select @XMLVar.exist('/catalog/book/@category')-----返回1  

       select @XMLVar.exist('/catalog/book1')-----返回0  

       set @XMLVar = null  

       select @XMLVar.exist('/catalog/book')-----返回null

    2.xml.value

       select @XMLVar.value('/catalog[1]/book[1]','varchar(MAX)')  

       select @XMLVar.value('/catalog[1]/book[2]/@category','varchar(MAX)')  

       select @XMLVar.value('/catalog[2]/book[1]','varchar(MAX)')  

       结果集为:  

       Windows Step By StepBill Zack49.99   Developer   NULL

    3.xml.query

       select @XMLVar.query('/catalog[1]/book')   

       select @XMLVar.query('/catalog[1]/book[1]')  

       select @XMLVar.query('/catalog[1]/book[2]/author')

       结果集分别为:
    <book category="ITPro">
      <title>Windows Step By Step</title>
      <author>Bill Zack</author>
      <price>49.99</price>
    </book>
    <book category="Developer">
      <title>Developing ADO .NET</title>
      <author>Andrew Brust</author>
      <price>39.93</price>
    </book>
    <book category="ITPro">
      <title>Windows Cluster Server</title>
      <author>Stephen Forte</author>
      <price>59.99</price>
    </book>


    <book category="ITPro">
      <title>Windows Step By Step</title>
      <author>Bill Zack</author>
      <price>49.99</price>
    </book>


    <author>Andrew Brust</author>

    4.xml.nodes

       select T.c.query('.') as result from @XMLVar.nodes('/catalog/book') as T(c)  

       select T.c.query('title') as result from @XMLVar.nodes('/catalog/book') as T(c)

       结果集分别为:
    <book category="ITPro"><title>Windows Step By Step</title><author>Bill …………

    <book category="Developer"><title>Developing ADO .NET</title><author>Andrew …………
    <book category="ITPro"><title>Windows Cluster Server</title><author>Stephen …………


    <title>Windows Step By Step</title>
    <title>Developing ADO .NET</title>
    <title>Windows Cluster Server</title>

     5.xml.modify

  • 相关阅读:
    Spring/Java error: namespace element 'annotation-config' … on JDK 1.5 and higher
    警告: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:Zhuo' did not find a matching property.
    快速学习Symfony4,Symfony4教程
    window下安装redis报错: creating server tcp listening socket 127.0.0.1:6379: bind No error
    Windows 安装nginx
    js焦点事件:onfocus、onblur、focus()、blur()、select()
    input 输入框只能输入纯数字
    随机抽取不重复的数组元素
    常用 书签 mark 常用网站
    仿真灯泡 电灯泡 灯光 光环闪烁 流星雨
  • 原文地址:https://www.cnblogs.com/cxd4321/p/2175402.html
Copyright © 2011-2022 走看看