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 ...
随机推荐
- HDU-4671 Backup Plan 构造解
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4671 假设是3 m,首先按照第一列按照1 2 3 1 2 3 1...排下去,然后个数就是一个 (m/ ...
- 10段实用的HTML5代码
1.HTML5编写的CSS ResetCSS Reset也可以写成Reset CSS,即重设浏览器样式. /* html5doctor.com Reset Stylesheet (Eric Mey ...
- Android实例-使用电话拨号器在移动设备上(官方)(XE8+小米2)
源文地址: http://docwiki.embarcadero.com/RADStudio/XE5/en/Mobile_Tutorial:_Using_the_Phone_Dialer_on_Mob ...
- 常用的各种标准下载网站(HB GB GJB MH)
标准分享网 http://www.bzfxw.com/ 标准下载网 http://www.bzxz.net/ 搜标准网 http://www.biaozhunw.com/Index.html 标准 ...
- C++学习笔记(五):指针和引用
声明指针: //指针声明 * 号左右的空格是可选的,下面的定义都是正确的 int *pointer1; int* pointer2; int*pointer3; int * pointer4; //注 ...
- Gitbook安装
Gitbook安装 Gitbook是从NMP安装的,命令行: $ npm install gitbook -g 安装完之后,你可以检验下是否安装成功: $ gitbook -V 0.4.2 如果你看到 ...
- JQuery.Ajax之错误调试帮助信息介绍
下面是Jquery中AJAX参数详细列表: timeout Number 设置请求超时时间(毫秒).此设置将覆盖全局设置. async Boolean (默认: true) 默认设置下,所有请求均为异 ...
- Codeforces Round #324 (Div. 2) B. Kolya and Tanya 快速幂
B. Kolya and Tanya Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/584/pro ...
- 2015北京网络赛 A题 The Cats' Feeding Spots 暴力
The Cats' Feeding Spots Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://hihocoder.com/contest/acm ...
- SVN安装笔记
1.先去以下网址去下载服务器端与客户端的SVN 2.服务端SVN的安装 点击next 点击next 再点击next 点击next 点击install 点击Finish,这样服务器端的SVN就安装好了, ...