zoukankan      html  css  js  c++  java
  • T-sql for xml path使用(转)

    参考:

    http://www.cnblogs.com/langhua/p/4193161.html

    //用法: FOR XML PATH 方法是用于将查询结果集以XML形式展示
    sql:
    select top 5 p.ContactTypeID,p.ModifiedDate,p.Name from [Person].[ContactType] p for xml path
    显示结果:
    <row>
      <ContactTypeID>1</ContactTypeID>
      <ModifiedDate>2002-06-01T00:00:00</ModifiedDate>
      <Name>Accounting Manager</Name>
    </row>
    <row>
      <ContactTypeID>2</ContactTypeID>
      <ModifiedDate>2002-06-01T00:00:00</ModifiedDate>
      <Name>Assistant Sales Agent</Name>
    </row>
    <row>
      <ContactTypeID>3</ContactTypeID>
      <ModifiedDate>2002-06-01T00:00:00</ModifiedDate>
      <Name>Assistant Sales Representative</Name>
    </row>
    <row>
      <ContactTypeID>4</ContactTypeID>
      <ModifiedDate>2002-06-01T00:00:00</ModifiedDate>
      <Name>Coordinator Foreign Markets</Name>
    </row>
    <row>
      <ContactTypeID>5</ContactTypeID>
      <ModifiedDate>2002-06-01T00:00:00</ModifiedDate>
      <Name>Export Administrator</Name>
    </row>
    path的用法:用来指定行结点,如果不指定行节点,默认使用'row',
    sql:
    select top 5 p.ContactTypeID,p.ModifiedDate,p.Name from [Person].[ContactType] p for xml path('AA')
    显示结果:
    <AA>
      <ContactTypeID>1</ContactTypeID>
      <ModifiedDate>2002-06-01T00:00:00</ModifiedDate>
      <Name>Accounting Manager</Name>
    </AA>
    <AA>
      <ContactTypeID>2</ContactTypeID>
      <ModifiedDate>2002-06-01T00:00:00</ModifiedDate>
      <Name>Assistant Sales Agent</Name>
    </AA>
    <AA>
      <ContactTypeID>3</ContactTypeID>
      <ModifiedDate>2002-06-01T00:00:00</ModifiedDate>
      <Name>Assistant Sales Representative</Name>
    </AA>
    <AA>
      <ContactTypeID>4</ContactTypeID>
      <ModifiedDate>2002-06-01T00:00:00</ModifiedDate>
      <Name>Coordinator Foreign Markets</Name>
    </AA>
    <AA>
      <ContactTypeID>5</ContactTypeID>
      <ModifiedDate>2002-06-01T00:00:00</ModifiedDate>
      <Name>Export Administrator</Name>
    </AA>
    下来来利用 for xml path 输出结果就是一行字符串,如果去掉行节点就可以把整列的结果输出一行,
    sql : select top 5 '['+ p.Name+']' from [Person].[ContactType] p for xml path('')
    显示结果: 
    [Accounting Manager][Assistant Sales Agent][Assistant Sales Representative][Coordinator Foreign Markets][Export Administrator]
    
    
  • 相关阅读:
    简单读取winfrom资源文件
    string.Format对C#字符串格式化
    如何在SQL中使用循环结构
    Linq to SQL 多条件动态组合查询(实战篇)
    提问的艺术
    XtraGrid 单元格加边框颜色
    凭证控件制作
    C# double 四舍五入
    自定义光标样式
    触发窗体事件(例如按Esc关闭窗体)
  • 原文地址:https://www.cnblogs.com/shy1766IT/p/7244900.html
Copyright © 2011-2022 走看看