SQL-学习使用FOR XML PATH
前言:本人SQL技术很烂,然后工作时间也不久,许多东西都还在学习中,说的不好的地方尽请谅解.
首先跟大家说一下我今天遇到的问题吧.

查出的数据有三列,第一列存放的是32位的GUID,Res_Name存放的是一个物资类型.Res_Data存放的是部门的GUID.我现在需要得到的数据是这样的.

首先大家可以看到.第一张图的Res_Data中有多个部门的GUID,中间用逗号隔开的.
我当时想到的愚蠢的办法就是
@MaterialTypeName nvarchar(200),
@CentralizedName nvarchar(200),
@start int,
@limit int,
@totalCount int output
AS
BEGIN
SET NOCOUNT ON;
select
ROW_NUMBER() over (order by res_id asc) as RowNumber,
*
into #List
from
UBIPlatform..T_RESOURCE
WHERE Res_Parent_Code='741cdd2bef2e479f8c5dd35cf6e8bf2a' declare @i int,@count int;
declare @Centralized nvarchar(200);
declare @List1 table(id int, ResId nvarchar(50),ResName nvarchar(50),ResData nvarchar(50));
select @count=COUNT(*) from UBIPlatform..T_RESOURCE WHERE Res_Parent_Code='741cdd2bef2e479f8c5dd35cf6e8bf2a'
set @i=1
while @i<=@count
begin
if @i in (select RowNumber from #List)
begin
set @Centralized='';
select
@Centralized=@Centralized+','+LTRIM(Res_Name)
from UBIPlatform.dbo.FN_GETMultiValTable(
(select
Res_Data
from
UBIPlatform..T_RESOURCE
where
Res_Id=(select Res_Id from #List where RowNumber=@i))) ge
inner join UBIPlatform..T_RESOURCE r on r.Res_Id=ge.nvalue if @Centralized!=''
begin
insert into
@List1
select
@i,
Res_Id,
Res_Name,
(RIGHT(@Centralized,LEN(@Centralized)-1))
from
UBIPlatform..T_RESOURCE
where Res_Id=(select Res_Id from #List where RowNumber=@i)
end
else
begin
insert into
@List1
select
@i,
Res_Id,
Res_Name,
@Centralized
from
UBIPlatform..T_RESOURCE
where Res_Id=(select Res_Id from #List where RowNumber=@i)
end
end
set @i=@i+1
end select ROW_NUMBER() over (order by id asc) as RowNumber,* into #List2 from @List1 where
(@MaterialTypeName is null or @MaterialTypeName = '' or ResName like '%'+@MaterialTypeName+'%')
and (@CentralizedName is null or @CentralizedName = '' or ResData like '%'+@CentralizedName+'%') select top(@limit) * from #List2 where RowNumber > @start order by RowNumber asc
select @totalCount =COUNT(1) from @List1
END
这个是我开始写出来的一个.烂到不行.虽然是解决了我的需求.但是显而易见这种办法是不可取的.后来请教同事,跟我介绍了FOR XML PATH,我查阅了一下就是将查询结果集以XML形式展现.
select Res_Id,Res_Name,Res_Data from UBIPlatform..T_RESOURCE where Res_Parent_Code='741cdd2bef2e479f8c5dd35cf6e8bf2a' FOR XML PATH
结果:
<row>
<Res_Id>239dbe35bd8446afb262f62712d8eb1b</Res_Id>
<Res_Name>修理费-设备备件</Res_Name>
<Res_Data>4BD2D7C9D91546B09BA4438EE583F682</Res_Data>
</row>
<row>
<Res_Id>4d35c89868854d649963410a126b4c30</Res_Id>
<Res_Name>低值易耗-计量器具</Res_Name>
<Res_Data>4ADEEC453DE04910B0136593CBB4187C</Res_Data>
</row>
<row>
<Res_Id>4e74469a37894ea8a7ddd5e356433119</Res_Id>
<Res_Name>物料消耗-计算机耗材</Res_Name>
<Res_Data>4BD2D7C9D91546B09BA4438EE583F682,9C87FFAFD8D24B5BBEA3BF1221DD507B</Res_Data>
</row>
<row>
<Res_Id>608f30860c16430aa8b13f98df0ca9f3</Res_Id>
<Res_Name>物料消耗-水票</Res_Name>
<Res_Data></Res_Data>
</row>
<row>
<Res_Id>87a4cefa112241c1b648454e7b3682d9</Res_Id>
<Res_Name>低值易耗-工具及其他</Res_Name>
<Res_Data></Res_Data>
</row>
<row>
<Res_Id>c2908fe510dd476aa878622dd9d07c83</Res_Id>
<Res_Name>物料消耗-杂品</Res_Name>
<Res_Data></Res_Data>
</row>
<row>
<Res_Id>c9014727c9804c6e9df4cc1bc1487a84</Res_Id>
<Res_Name>劳动保护费-劳保用品</Res_Name>
<Res_Data>D5566FDCDBB448FAB4A48D20A2492626</Res_Data>
</row>
<row>
<Res_Id>d3397fdcb454440f88c9c4f9432b3f40</Res_Id>
<Res_Name>低值易耗-办公设施</Res_Name>
<Res_Data>9C87FFAFD8D24B5BBEA3BF1221DD507B</Res_Data>
</row>
<row>
<Res_Id>d8222bcaeaba460db945324cb0a93a23</Res_Id>
<Res_Name>修理费-计算机备件</Res_Name>
<Res_Data>4BD2D7C9D91546B09BA4438EE583F682</Res_Data>
</row>
那么,如何改变XML行节点的名称呢?代码如下:
select Res_Id,Res_Name,Res_Data from UBIPlatform..T_RESOURCE where Res_Parent_Code='741cdd2bef2e479f8c5dd35cf6e8bf2a' FOR XML PATH('RESOURCE')
原来的行节点<row> 变成了我们在PATH后面括号()中,自定义的名称<RESOURCE>,结果如下:
<RESOURCE>
<Res_Id>239dbe35bd8446afb262f62712d8eb1b</Res_Id>
<Res_Name>修理费-设备备件</Res_Name>
<Res_Data>4BD2D7C9D91546B09BA4438EE583F682</Res_Data>
</RESOURCE>
<RESOURCE>
<Res_Id>4d35c89868854d649963410a126b4c30</Res_Id>
<Res_Name>低值易耗-计量器具</Res_Name>
<Res_Data>4ADEEC453DE04910B0136593CBB4187C</Res_Data>
</RESOURCE>
<RESOURCE>
<Res_Id>4e74469a37894ea8a7ddd5e356433119</Res_Id>
<Res_Name>物料消耗-计算机耗材</Res_Name>
<Res_Data>4BD2D7C9D91546B09BA4438EE583F682,9C87FFAFD8D24B5BBEA3BF1221DD507B</Res_Data>
</RESOURCE>
<RESOURCE>
<Res_Id>608f30860c16430aa8b13f98df0ca9f3</Res_Id>
<Res_Name>物料消耗-水票</Res_Name>
<Res_Data></Res_Data>
</RESOURCE>
<RESOURCE>
<Res_Id>87a4cefa112241c1b648454e7b3682d9</Res_Id>
<Res_Name>低值易耗-工具及其他</Res_Name>
<Res_Data></Res_Data>
</RESOURCE>
<RESOURCE>
<Res_Id>c2908fe510dd476aa878622dd9d07c83</Res_Id>
<Res_Name>物料消耗-杂品</Res_Name>
<Res_Data></Res_Data>
</RESOURCE>
<RESOURCE>
<Res_Id>c9014727c9804c6e9df4cc1bc1487a84</Res_Id>
<Res_Name>劳动保护费-劳保用品</Res_Name>
<Res_Data>D5566FDCDBB448FAB4A48D20A2492626</Res_Data>
</RESOURCE>
<RESOURCE>
<Res_Id>d3397fdcb454440f88c9c4f9432b3f40</Res_Id>
<Res_Name>低值易耗-办公设施</Res_Name>
<Res_Data>9C87FFAFD8D24B5BBEA3BF1221DD507B</Res_Data>
</RESOURCE>
<RESOURCE>
<Res_Id>d8222bcaeaba460db945324cb0a93a23</Res_Id>
<Res_Name>修理费-计算机备件</Res_Name>
<Res_Data>4BD2D7C9D91546B09BA4438EE583F682</Res_Data>
</RESOURCE>
OK,接下来我就用这个FOR XML PATH用在我的问题上,
@MaterialTypeName nvarchar(200),
@CentralizedName nvarchar(200),
@start int,
@limit int,
@totalCount int output
AS
BEGIN
SET NOCOUNT ON;
select
t_r.Res_Id as ResId,
t_r.Res_Name as ResName,
STUFF((select ','+t_r1.Res_Name
from UBIPlatform.dbo.T_RESOURCE t_r1 where t_r1.Res_Id in
(select nvalue from UBIPlatform.dbo.FN_GETMultiValTable(t_r.Res_Data))
for xml path('')),1,1,'') as ResData
into
#List1
from
UBIPlatform..T_RESOURCE t_r
where Res_Parent_Code='741cdd2bef2e479f8c5dd35cf6e8bf2a' select
ROW_NUMBER() over (order by ResId asc) as RowNumber,
* into #List2 from #List1
where (@MaterialTypeName is null or @MaterialTypeName = '' or ResName like '%'+@MaterialTypeName+'%')
and (@CentralizedName is null or @CentralizedName = '' or ResData like '%'+@CentralizedName+'%') select top(@limit) * from #List2 where RowNumber > @start order by RowNumber asc
select @totalCount =COUNT(1) from #List2
END
就这样,才几行的SQL就把我的问题解决了.
SQL-学习使用FOR XML PATH的更多相关文章
- sql server 使用for xml path 将1列多行转换为字符串连接起来,俗称 sql 合并字符
由于项目的原因,需要将一些记录分类汇总,但还要列出相关的明细,这样的需求我还是第一次遇到,蛋疼了,还是请求一下度娘吧.搜索一番还是有结果,请看以下例子: create table tb ([id] i ...
- sql中的for xml path() 实现字符串拼接
通常我们需要在sql中拼接字符串 ,可以用for xml path() 来进行拼接,如下实例. 同时未去掉最后一个逗号可以用LEFT函数来实现. ) AS UserName FRO ...
- sql server 使用for xml path 将1列多行转换为字符串连接起来
create table tb ([id] )) insert into tb ,'aa' union all ,'bb' union all ,'cc' union all ,'dd' union ...
- sql server的for xml path与变通的行转列
SQL Server中有提供一个FOR XML PATH的子句(不知道能不能叫函数),用来将查询结果行输出成XML格式,我们可以通过这个语法做一些变通实现一些特定的功能,比如说行转列.要会变通的话,当 ...
- SQL字符串拼接FOR XML PATH
在工作中难免会遇到数据库中数据要进行拼接的问题,字符串拼接可以是用SQL的拼接也可以使用C#的拼接,本次说的是使用SQL进行拼接. 首先插入测试语句: --测试语句,准备创建表的语句:如下 CREAT ...
- Sql Server 之 for xml (path,raw,auto,root)
1.for xml path('str') select ID,CreateTime from dbo.ArticleInfo for xml Path('mytitle') 结果:(注意:如果是s ...
- sql查询语句for xml path语法
[原地址] for xml path作用:将多行的查询结果,根据某一些条件合并到一行. 例:现有一张表 执行下面语句 select Department, (SELECT Employee+',' F ...
- 数据库学习:for xml path
一.开发环境 数据库:SQLServer2012 二.语法简介 for xml path它以xml形式展示查询的结果集 三.语法介绍 现在数据库中有一张表 1.基本语法 select * from B ...
- (4.28)for xml path 在合并拆分上的作用演示
for xml path 用于合并与拆分 1.合并 很多时候需要在SQL Server中创建逗号分隔列表.这可以使用SQL Server的DOR XML PATH功能完成.与select语句一起使用时 ...
- Sql server For XML Path 学习
最近看到太多人问这种问题 自己也不太了解 就在网上学习学习 自己测试一番 CREATE TABLE test0621 (id INT,NAME NVARCHAR(max)) INSERT tes ...
随机推荐
- PHP编码规范整理,很全很实用(图文版)
有一个组织叫做“php互操作性框架制定小组”,这个小组的主要目的是制定各种PHP编码规范的,下面就是我根据小组提供的建议整理的一些常用的编码规范. PSR-1: 1.PHP代码文件必须以<?ph ...
- HDU-4696 Answers 纯YY
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4696 题意:给一个图,每个点的出度为1,每个点的权值为1或者2.给n个询问,问是否能找到一条路径的权值 ...
- SPOJ-7001 VLATTICE 莫比乌斯反演定理
题目链接:http://www.spoj.com/problems/VLATTICE/ 题意:求gcd(x,y,z)=1,1<=x,y,z<=n,的个数. 开始做的时候枚举gcd(x,y) ...
- HDU-4604 Deque DP
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4604 因为deque最后的数列是单调不降的,因此,我们可以枚举数列中的某个中间数Ai,如果从中间数Ai ...
- berserkJS(大名:狂暴JS / 昵称:疯子JS)
极分享:高质分享+专业互助=没有难做的软件+没有不得已的加班 http://www.finalshares.com/read-6763?f=g-20
- jQuery的遍历方法
1.jQuery中的map使用方法 <!DOCTYPE html> <html> <head> <style>p { color:red; }</ ...
- btrace拓展工具-java应用性能诊断优化利器
Btrace是一个实时监控工具,可以无需修改应用代码(事实上它修改了字节码),来达到不可告人的秘密!这是性能调优和诊断的利器! 它可以获取应用程序代码的执行时间,他可以让你无需修改代码,帮你做时间的打 ...
- 关于PKCS5Padding与PKCS7Padding的区别
国内私募机构九鼎控股打造APP,来就送 20元现金领取地址:http://jdb.jiudingcapital.com/phone.html内部邀请码:C8E245J (不写邀请码,没有现金送)国内私 ...
- powershell.exe直接运行命令
powershell.exe -Command "& { ('time={0},user={1}' -f (get-date),(whoami)) | Out-File " ...
- 【转】SQL语句中的正则表达示
正则表达式作用是匹配方本,将一个模式(正则表达式)与一个文本串进行比较. MySQL用WHERE子句对正则表达式提供了初步的支持,允许你指定用正则表达式过滤SELECT检索出的数据. MySQL仅支持 ...