SQL Server FOR XML PATH 语句的应用
在SQL Server中利用 FOR XML PATH 语句能够把查询的数据生成XML数据,下面是它的一些应用示例。
1
2
3
4
5
|
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
|
运行这段脚本,将生成如下结果:
1
2
3
4
5
6
7
8
|
< row >
< UserID >1</ UserID >
< UserName >a</ UserName >
</ row >
< row >
< UserID >2</ UserID >
< UserName >b</ UserName >
</ row >
|
1
|
大家可以看到两行数据生成了两个节点,修改一下PATH的参数: |
1
|
select UserID,UserName from @TempTable FOR XML PATH( 'lzy' )
|
再次运行上述脚本,将生成如下的结果:
1
2
3
4
5
6
7
8
|
< lzy >
< UserID >1</ UserID >
< UserName >a</ UserName >
</ lzy >
< lzy >
< UserID >2</ UserID >
< UserName >b</ UserName >
</ lzy >
|
1
|
可以看到节点变成,其实PATH() 括号内的参数是控制节点名称的,这样的话大家可以看一下如果是空字符串(不是没有参数)会是什么结果? |
1
|
select UserID,UserName from @TempTable FOR XML PATH( '' )
|
执行上面这段脚本将生成结果:
1
2
3
4
|
< UserID >1</ UserID >
< UserName >a</ UserName >
< UserID >2</ UserID >
< UserName >b</ UserName >
|
这样就不显示上级节点了,大家知道在 PATH 模式中,列名或列别名被作为 XPath 表达式来处理,也就是说,是列的名字,这样大胆试验一下不给指定列名和别名会是怎么样?
1
|
select CAST (UserID AS varchar ) + '' ,UserName + '' from @TempTable FOR XML PATH( '' )
|
运行上面这句将生成结果
1a2b
所有数据都生成一行,而且还没有连接字符,这样的数据可能对大家没有用处,还可以再变化一下:
1
|
select CAST (UserID AS varchar ) + ',' ,UserName + '' , ';' from @TempTable FOR XML PATH( '' )
|
生成结果
1,a;2,b;
大家现在明白了吧,可以通过控制参数来生成自己想要的结果,例如:
1
|
select '{' + CAST (UserID AS varchar ) + ',' , '"' +UserName + '"' , '}' from @TempTable FOR XML PATH( '' )
|
生成结果
{1,"a"}{2,"b"}
还可以生成其他格式,大家可以根据自己需要的格式进行组合。
下面是一个数据统计的应用,希望大家可以通过下面的实例想到更多的应用
1
2
3
4
5
6
7
8
9
10
11
12
13
|
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
相关推荐
从数据库里面取数据,直接生成XML文件 ,可以自定义查询表而导出数据到XML文件里面
通过这个可执行的文件,写入SQL语句然后查询出来数据自动生成XML格式,并可以提供给FTvantagepoint中dashboard动态数据连接,这样就可以在不配置ASP服务器的情况下实现dashboard的SQL查询关系数据库。
sqlserver to XML 将数据库的表转成XML
LINQ TO SQL ,LINQ TO XML,LINQ中文教程,是强人书写的,制作有点粗糙,但是,是好教材
一个用Python编写的cgi程序,它将HTTP GET和POST查询转换为SQL查询,运行它们,将结果转换为XML,然后将XML作为对HTTP查询的响应返回。 它还支持返回单个BLOB作为整个响应。
本书部分介绍了LINQ技术及C#和VB为此提供的新语言特性,第二、三、四部分详细介绍了LINQ的主要功能LINQ to Objects、LINQ to SQL、LINQ to XML,第五部分研究了LINQ的可扩展性、在应用程序中使用各种LINQ功能的方法...
Linq 大全 Linq To Sql Linq To DataSet Linq To Object Linq to Xml
Linq 教程Linq To Xml 和Linq To Sql
Linq开发宝典 LInq入门宝典 Linq To Xml linq to sql
Linq to sql(或者叫DLINQ)是LINQ(.NET语言集成查询)的一部分,全称基于关系数据的 .NET 语言集成查询,用于以对象形式管理关系数据,并提供了丰富的查询功能,它和Linq to xml、Linq to objects、Linq to dataset、...
VS2008的发布,最激动人心的不过是LINQ的诞生。LINQ,语言集成查询(Language INtegrated Query)是一组用于c#和Visual Basic语言的扩展。它允许编写C#或者Visual Basic代码以查询数据库相同的方式操作内存数据。
学习LINQ的目前国内现有的最好的资源,有LINQ to xml的精讲,LINQ 的初级,高级学习,是学习LINQ的好资源。
introduce how to use the xml in sql server
This focus of this book is in three primary areas. First and foremost is ...and changes to the .NET Framework and ADO.NET for the support of the new xml data type and CLR integration in SQL Server 2005.
In Part 1 of this book, I will cover and provide the recipes on how to work with the SQL Server XML data type; discuss and demonstrate real type scenarios to load, build, and shred the XML; and ...
I wrote this book for anyone who needs to understand how to use XML to integrate applications or organizations that store data in a SQL Server database. The book covers XML-related functionality in ...
Learn to be proficient with xml. understand its ins and outs
The book includes especially thorough coverage of Transact-SQL stored procedure programming, including features such as extended procedures, database design, and XML that are often disregarded in ...
结合在ASP.NET中使用Linq to Sql,我们可以非常方便地将XML文件存储到SQL Server数据库中。但是在默认情况下,如果你存储的XML文件比较大(超过2MB),在SQL Server管理器中不能直接点击查看XML内容而报以下错误提示...