zoukankan      html  css  js  c++  java
  • SQL查询 FOR XML [RAW|AUTO|EXPLICIT]

    1.FOR XML RAW
    2.FOR XML AUTO
    3.FOR XML EXPLICIT
    4.更改显示Tag为中文
    5.同表 多层
    6.异表 多层
    7.处理数据及日期等的Null值
    ==================
    数据库Person表中的数据为
    personName personAge  
    lisi                    30 
    zhangsan        30 

    1. ----------[ RAW ]---------
    SELECT [personName], [personAge]
    FROM [TestDB].[dbo].[person]
    FOR XML RAW

    结果:
    <row personName="lisi      " personAge="30"/>
    <row personName="zhangsan  " personAge="30"/>

    2.----------[ AUTO ]--------
    SELECT [personName], [personAge]
    FROM [TestDB].[dbo].[person]
    FOR XML AUTO

    结果:
    <TestDB.dbo.person personName="lisi      " personAge="30"/>
    <TestDB.dbo.person personName="zhangsan  " personAge="30"/>


    3.-----------[ EXPLICIT ]--------
    select 1 as Tag ,null as parent
    ,RTRIM(personName) as [PersonBasic!1!personName]
    ,RTRIM(personAge) as [PersonBasic!1!personAge!xml]
    FROM [TestDB].[dbo].[person]
    FOR XML EXPLICIT

    结果:

    <PersonBasic personName="lisi">
       <personAge>30</personAge>
    </PersonBasic>
    <PersonBasic personName="zhangsan">
       <personAge>30</personAge>
    </PersonBasic>

    4.----- 更改显示Tag为中文 -----
    select
    1 as tag,
    null as parent,
    personName  as [人员!1!姓名!xml],
    personAge  as [人员!1!年龄!xml]
    from person
    for xml EXPLICIT

    结果:

    <人员>
    <姓名>lisi      </姓名>
    <年龄>30</年龄>
    </人员>
    <人员>
    <姓名>zhangsan  </姓名>
    <年龄>30</年龄>
    </人员>

    5. ----- 同表 多层 ----
    select
    1 as tag,
    null as parent,
    rtrim(A.personName)   as [人员!1!姓名],
    null as [人员信息!2!年龄!xml]
    from person A

    union all

    select
    2 as tag,
    1 as parent,
    rtrim(A.personName),
    B.personAge
    from person B,person A
    where A.personName=B.personName

    order by [人员!1!姓名],tag

    for xml EXPLICIT

    结果:

    <人员 姓名="lisi">
         <人员信息>
             <年龄>30</年龄>
         </人员信息>
    </人员>
    <人员 姓名="zhangsan">
         <人员信息>
             <年龄>30</年龄>
         </人员信息>
    </人员>

    6.-------- 异表 多层 -------
    select
    1 as tag,
    null as parent,
    rtrim(A.personName)   as [人员!1!姓名],
    null as [人员信息!2!年龄!xml],
    null as [人员信息!2!职业!xml]
    from person A

    union all

    select
    2 as tag,
    1 as parent,
    rtrim(A.personName),
    B.personAge,
    rtrim(B.personJob)
    from personInfo B,person A
    where B.personName=A.personName
    order by [人员!1!姓名],tag
    for xml EXPLICIT

    结果:

    <人员 姓名="lisi">
         <人员信息>
             <年龄>30</年龄>
             <职业>teacher</职业>
         </人员信息>
    </人员>
    <人员 姓名="zhangsan">
          <人员信息>
              <年龄>30</年龄>
              <职业>worker</职业>
          </人员信息>
    </人员>

    7.-------处理数据及日期等的Null值-------
    数据库Person表中的数据为
    personName personAge   personBirth(可为空)
    lisi                     30     1987-06-06
    zhangsan        30     

    当数据表中的字段为Null值时
    产生的xml文档中 将没有该节点
    为了解决这一问题
    在必要的时候 可以将数字及日期类型
    转换为字符串类型
    这样就可以接收空串
    (但不知实际运用中效果如何)

    SELECT
    1 as TAG,
    null as parent,
    RTRIM(personName)  AS [人员!1!姓名!xml],
    RTRIM(ISNULL(CONVERT(CHAR,personAge),''))  AS [人员!1!年龄!xml],
    RTRIM(ISNULL(CONVERT(CHAR(10),personBirth,120),'')) AS [人员!1!出生日期!xml]
    FROM person
    FOR XML EXPLICIT

    结果:

    <人员>
       <姓名>lisi</姓名>
       <年龄>30</年龄>
       <出生日期>1987-06-06</出生日期>
    </人员>
    <人员>
       <姓名>zhangsan</姓名>
       <年龄></年龄>
       <出生日期></出生日期>
    </人员>

     

  • 相关阅读:
    App架构师实践指南四之性能优化一
    App架构师实践指南三之基础组件
    App架构师实践指南二之App开发工具
    App架构师实践指南一之App基础语法
    Linux下阅读MHT文件
    What Is Docker & Docker Container ? A Deep Dive Into Docker !
    Difference between Docker Image and Container?
    RabbitMQ .NET/C# Client API Guide
    How RabbitMQ Works and RabbitMQ Core Concepts
    Message Queue vs Message Bus — what are the differences?
  • 原文地址:https://www.cnblogs.com/freeliver54/p/683563.html
Copyright © 2011-2022 走看看