• sql for xml


    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</姓名>
    <年龄></年龄>
    <出生日期></出生日期>
    </人员>

  • 相关阅读:
    Nuxt.js 踩坑记录(2) 使用sequelize时,提示install mysql2,安装了仍然不能解决问题
    Nuxt.js 踩坑记录,(1)引入fs包报错
    JS手写call、bind、apply
    手写Promise简易版
    generator函数
    ["1","2","3"].map(parseInt)结果
    改变对象转换为原始值的方式
    instanceof判断问题
    e.target和e.currentTarget区别
    java设计模式--适配器模式
  • 原文地址:https://www.cnblogs.com/sunth/p/2334336.html
Copyright © 2020-2023  润新知