惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

I
Intezer
V
Vulnerabilities – Threatpost
Google Online Security Blog
Google Online Security Blog
T
The Exploit Database - CXSecurity.com
C
CXSECURITY Database RSS Feed - CXSecurity.com
AWS News Blog
AWS News Blog
G
GRAHAM CLULEY
P
Privacy & Cybersecurity Law Blog
www.infosecurity-magazine.com
www.infosecurity-magazine.com
C
Cybersecurity and Infrastructure Security Agency CISA
N
News | PayPal Newsroom
T
Tenable Blog
Spread Privacy
Spread Privacy
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
S
Secure Thoughts
P
Privacy International News Feed
IT之家
IT之家
Project Zero
Project Zero
T
The Blog of Author Tim Ferriss
Engineering at Meta
Engineering at Meta
大猫的无限游戏
大猫的无限游戏
博客园_首页
GbyAI
GbyAI
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
量子位
雷峰网
雷峰网
Apple Machine Learning Research
Apple Machine Learning Research
Hacker News: Ask HN
Hacker News: Ask HN
Google DeepMind News
Google DeepMind News
MongoDB | Blog
MongoDB | Blog
N
Netflix TechBlog - Medium
Martin Fowler
Martin Fowler
NISL@THU
NISL@THU
I
InfoQ
D
DataBreaches.Net
有赞技术团队
有赞技术团队
K
Kaspersky official blog
Security Latest
Security Latest
The Register - Security
The Register - Security
Hugging Face - Blog
Hugging Face - Blog
S
Security @ Cisco Blogs
P
Proofpoint News Feed
M
MIT News - Artificial intelligence
H
Hackread – Cybersecurity News, Data Breaches, AI and More
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
AI
AI
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
P
Proofpoint News Feed
Security Archives - TechRepublic
Security Archives - TechRepublic
N
News and Events Feed by Topic

博客园 - 飘渺峰

一致性Hash算法 .net Parallel并行使用注意事项 析构函数和Dispose方法的区别 查看SQLServer的最大连接数 Hash算法-CityHash算法 Hash算法 Sunday算法--C#版 KMP算法--C#版 BoyerMoore(BM)算法--C# HostFileChangeMonitor [转]软件项目管理总体流程设计 全排列和组合算法 生活 负载均衡算法--C#版 结束进程的方法forceStopPackage 【转】OAUTH协议简介 nlog轻量级日志组件 反射加载程序集的几个方法的区别 windows 下TCP最大连接数
SQL Server FOR XML PATH 语句的应用
飘渺峰 · 2010-12-24 · via 博客园 - 飘渺峰

经常在论坛看到高手使用了 for xml path,由于是搜索一下,记录了详细的使用方法。
在SQL Server中利用 FOR XML PATH 语句能够把查询的数据生成XML数据,下面是它的一些应用示例。


DECLARE @TempTable table(UserID int , UserName nvarchar(50));
insert into @TempTable (UserID,UserName) values (1,'a')
insert into @TempTable (UserID,UserName) values (2,'b')
 
select UserID,UserName from @TempTable FOR XML PATH运行这段脚本,将生成如下结果:<row>
  <UserID>1</UserID>
  <UserName>a</UserName>
</row>
<row>
  <UserID>2</UserID>
  <UserName>b</UserName>
</row>大家可以看到两行数据生成了两个节点,修改一下PATH的参数:select UserID,UserName from @TempTable FOR XML PATH('lzy')再次运行上述脚本,将生成如下的结果:

<lzy>
  <UserID>1</UserID>
  <UserName>a</UserName>
</lzy>
<lzy>
  <UserID>2</UserID>
  <UserName>b</UserName>
</lzy>可以看到节点变成,其实PATH() 括号内的参数是控制节点名称的,这样的话大家可以看一下如果是空字符串(不是没有参数)会是什么结果?select UserID,UserName from @TempTable FOR XML PATH('')执行上面这段脚本将生成结果:

<UserID>1</UserID>
<UserName>a</UserName>
<UserID>2</UserID>
<UserName>b</UserName>这样就不显示上级节点了,大家知道在 PATH 模式中,列名或列别名被作为 XPath 表达式来处理,也就是说,是列的名字,这样大胆试验一下不给指定列名和别名会是怎么样?

select CAST(UserID AS varchar) + '',UserName + '' from @TempTable FOR XML PATH('') 

运行上面这句将生成结果 

1a2b

所有数据都生成一行,而且还没有连接字符,这样的数据可能对大家没有用处,还可以再变化一下:
select CAST(UserID AS varchar) + ',',UserName + '',';' from @TempTable FOR XML PATH('') 
生成结果 
1,a;2,b;
大家现在明白了吧,可以通过控制参数来生成自己想要的结果,例如: 
select '{' + CAST(UserID AS varchar) + ',','"' +UserName + '"','}' from @TempTable FOR XML PATH('') 
生成结果 
{1,"a"}{2,"b"}
还可以生成其他格式,大家可以根据自己需要的格式进行组合。 
下面是一个数据统计的应用,希望大家可以通过下面的实例想到更多的应用 
DECLARE @T1 table(UserID int , UserName nvarchar(50),CityName nvarchar(50));
insert into @T1 (UserID,UserName,CityName) values (1,'a','上海')
insert into @T1 (UserID,UserName,CityName) values (2,'b','北京')
insert into @T1 (UserID,UserName,CityName) values (3,'c','上海')
insert into @T1 (UserID,UserName,CityName) values (4,'d','北京')
insert into @T1 (UserID,UserName,CityName) values (5,'e','上海')
 
SELECT B.CityName,LEFT(UserList,LEN(UserList)-1) FROM (
SELECT CityName,
  (SELECT UserName+',' FROM @T1 WHERE CityName=A.CityName FOR XML PATH('')) AS UserList
FROM @T1 A 
GROUP BY CityName
) B 

生成结果(每个城市的用户名) 

北京 b,d
上海 a,c,e

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/bin_520_yan/archive/2010/11/18/6019608.aspx