• sql 和xml


    一·常用基本功能

    表结果:

    1.

    复制代码
     1 <row>
     2   <ID>1035</ID>
     3   <Rate>6.12</Rate>
     4   <Date>2013-11-28</Date>
     5   <Remark>正常</Remark>
     6 </row>
     7 <row>
     8   <ID>1036</ID>
     9   <Rate>6.15</Rate>
    10   <Date>2013-11-28</Date>
    11   <Remark>正常</Remark>
    12 </row>
    复制代码
    1 SELECT * 
    2 FROM dbo.T_H_ExchangeRate
    3 FOR XML PATH

    2.

    复制代码
    1 <ID>1035</ID>
    2 <Rate>6.12</Rate>
    3 <Date>2013-11-28</Date>
    4 <Remark>正常</Remark>
    5 <ID>1036</ID>
    6 <Rate>6.15</Rate>
    7 <Date>2013-11-28</Date>
    8 <Remark>正常</Remark>
    复制代码
    1 SELECT * 
    2 FROM dbo.T_H_ExchangeRate
    3 FOR XML PATH('')

    3.

    复制代码
    <SUN>
      <ID>1035</ID>
      <Rate>6.12</Rate>
      <Date>2013-11-28</Date>
      <Remark>正常</Remark>
    </SUN>
    <SUN>
      <ID>1036</ID>
      <Rate>6.15</Rate>
      <Date>2013-11-28</Date>
      <Remark>正常</Remark>
    </SUN>
    复制代码
    SELECT * 
    FROM dbo.T_H_ExchangeRate
    FOR XML PATH('SUN')

    4.

    复制代码
    <汉语>
      <序号>1035</序号>
      <汇率>6.12</汇率>
      <日期>2013-11-28</日期>
      <标记>正常</标记>
    </汉语>
    <汉语>
      <序号>1036</序号>
      <汇率>6.15</汇率>
      <日期>2013-11-28</日期>
      <标记>正常</标记>
    </汉语>
    复制代码
    复制代码
    SELECT ID AS '序号',
           Rate AS '汇率',
           Date AS '日期',
           Remark AS '标记'
    FROM dbo.T_H_ExchangeRate
    FOR XML PATH('汉语')
    复制代码

    5.

    复制代码
    --生成带顶层节点
    <Table> <Row> <ID>1035</ID> <Rate>6.12</Rate> <Date>2013-11-28</Date> <Remark>正常</Remark> </Row> <Row> <ID>1036</ID> <Rate>6.15</Rate> <Date>2013-11-28</Date> <Remark>正常</Remark> </Row> </Table>
    复制代码
    SELECT *
    FROM dbo.T_H_ExchangeRate
    FOR XML PATH('Row'),ROOT('Table')
    复制代码
    --还原成DataTable的形式,查找节点
    SELECT T.x.value('(ID)[1]','nvarchar(50)') AS ID,
           T.x.value('(Rate)[1]','nvarchar(50)') AS Rate,
           T.x.value('(Date)[1]','nvarchar(50)') AS Date,
           T.x.value('(Remark)[1]','nvarchar(50)') AS Remark
    FROM @xmlStr.nodes('/Table/Row') T(x)
    复制代码

    6.

    [正常],[正常],
    SELECT '['+Remark+'],'
    FROM dbo.T_H_ExchangeRate
    FOR XML PATH('')

    7.

    数据源:

    查询结果:

    复制代码
    --源代码
    SELECT C.O_USER, LEFT(C.Operate,LEN(Operate)-1) Operate FROM ( SELECT A.O_USER, ( SELECT B.O_TYPE+',' FROM dbo.T_UP_Olog AS B WHERE B.O_USER = A.O_USER FOR XML PATH('') ) Operate FROM dbo.T_UP_Olog A GROUP BY A.O_USER )AS C
    复制代码
  • 相关阅读:
    bzoj1625 / P2871 [USACO07DEC]手链Charm Bracelet
    bzoj1623 / P2909 [USACO08OPEN]牛的车Cow Cars
    bzoj1622 / P2908 [USACO08OPEN]文字的力量Word Power
    bzoj1621 / P2907 [USACO08OPEN]农场周围的道路Roads Around The Farm
    bzoj1620 / P2920 [USACO08NOV]时间管理Time Management
    [3.10校内训练赛]
    [bzoj1084][SCOI2005]最大子矩阵
    [bzoj1500][NOI2005]维修数列
    bzoj省选十连测推广赛
    多项式插值学习记录
  • 原文地址:https://www.cnblogs.com/yujihaia/p/7397949.html
Copyright © 2020-2023  润新知