T-sql for xml path使用(转)

摘要:
参考资源:http://www.cnblogs.com/langhua/p/4193161.html//用法:FORXMLPATH方法用于以XML显示查询结果集。SQL:p.姓名来自[人物]。[ContactType]pforxmlpath显示结果:AccountingManager<00<路径用法:用于指定行节点。如果未指定行节点,则默认使用“row”,

参考:

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]

免责声明:文章转载自《T-sql for xml path使用(转)》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇Nginx 反向代理配置4. Go语言—值类型和引用类型下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

osgEarth学习笔记

osgEarth学习笔记     这是个人在学习osgEarth时根据体会或从别的地方看到的做的一个简单整理,有些东东就是官方文档翻译过来的,都是根据自己的需要感觉需要记录下来什么东西就随便记录下来了,也没有个条理,都是用到哪记到哪,别见怪。对个人在初期使用osgEarth时有很多帮助,所以特发上来,希望对大家也有帮助 osgEarth学习笔记 1.   ...

Tensorflow:ImportError: DLL load failed: 找不到指定的模块 Failed to load the native TensorFlow runtime

配置: Windows 10 python3.6 CUDA 10.1 CUDNN 7.6.0 tensorflow 1.12 过程:import tensorflow as tf ,然后报错: Traceback (most recent call last): File "<ipython-input-6-64156d691fe5>...

vue+element ui 的时间控件选择 年月日时分

 前言:工作中用到 vue+element ui 的前端框架,需要选择年月日时分,但element ui官网demo有没有,所以记录一下。转载请注明出处:https://www.cnblogs.com/yuxiaole/p/9304510.html 网站地址:我的个人vue+element ui demo网站  github地址:yuleGH github...

Sublime text 3 汉化教程

首先,需要安装Package Control 启动并进入sublime主界面,打开Sublime Text的控制台(快捷键 ctrl + ~) 然后我们到Package Control官方网站,复制sublime text 3的脚本文件 网址:https://packagecontrol.io/installation 脚本文件如下: import u...

Python之字符编码与文件操作

目录 字符编码 Python2和Python3中字符串类型的差别 文件操作 文件操作的方式 文件内光标的移动 文件修改 字符编码 什么是字符编码? ''' 字符编码就是制定的一个将人类的语言的字符与二进制数据一一对应地翻译过来的标准。 ''' 字符编码的发展史与分类: 计算机最早的字符编码为ASCII,只规定了英文字母、数字和一些特殊字符与数字一一...

node连接mysql数据库出现Client does not support authentication protocol requested by server;的错误

在安装完MySQL的时候,我们现在一般都使用Navicat来连接数据库,可惜出现下面的错误:1251-Client does not support authentication protocol requested by server; consider upgrading MySQL client。 出现上述问题的原因是:mysql8 之前的版本中加密...