前言:本人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的更多相关文章

  1. sql server 使用for xml path 将1列多行转换为字符串连接起来,俗称 sql 合并字符

    由于项目的原因,需要将一些记录分类汇总,但还要列出相关的明细,这样的需求我还是第一次遇到,蛋疼了,还是请求一下度娘吧.搜索一番还是有结果,请看以下例子: create table tb ([id] i ...

  2. sql中的for xml path() 实现字符串拼接

       通常我们需要在sql中拼接字符串   ,可以用for xml path() 来进行拼接,如下实例. 同时未去掉最后一个逗号可以用LEFT函数来实现.     ) AS UserName  FRO ...

  3. sql server 使用for xml path 将1列多行转换为字符串连接起来

    create table tb ([id] )) insert into tb ,'aa' union all ,'bb' union all ,'cc' union all ,'dd' union ...

  4. sql server的for xml path与变通的行转列

    SQL Server中有提供一个FOR XML PATH的子句(不知道能不能叫函数),用来将查询结果行输出成XML格式,我们可以通过这个语法做一些变通实现一些特定的功能,比如说行转列.要会变通的话,当 ...

  5. SQL字符串拼接FOR XML PATH

    在工作中难免会遇到数据库中数据要进行拼接的问题,字符串拼接可以是用SQL的拼接也可以使用C#的拼接,本次说的是使用SQL进行拼接. 首先插入测试语句: --测试语句,准备创建表的语句:如下 CREAT ...

  6. Sql Server 之 for xml (path,raw,auto,root)

    1.for xml path('str') select ID,CreateTime  from dbo.ArticleInfo for xml Path('mytitle') 结果:(注意:如果是s ...

  7. sql查询语句for xml path语法

    [原地址] for xml path作用:将多行的查询结果,根据某一些条件合并到一行. 例:现有一张表 执行下面语句 select Department, (SELECT Employee+',' F ...

  8. 数据库学习:for xml path

    一.开发环境 数据库:SQLServer2012 二.语法简介 for xml path它以xml形式展示查询的结果集 三.语法介绍 现在数据库中有一张表 1.基本语法 select * from B ...

  9. (4.28)for xml path 在合并拆分上的作用演示

    for xml path 用于合并与拆分 1.合并 很多时候需要在SQL Server中创建逗号分隔列表.这可以使用SQL Server的DOR XML PATH功能完成.与select语句一起使用时 ...

  10. Sql server For XML Path 学习

    最近看到太多人问这种问题   自己也不太了解  就在网上学习学习 自己测试一番 CREATE TABLE test0621 (id INT,NAME NVARCHAR(max)) INSERT tes ...

随机推荐

  1. hadoop面试题答案

    Hadoop 面试题,看看书找答案,看看你能答对多少(2) 1. 下面哪个程序负责 HDFS 数据存储.a)NameNode  b)Jobtracker  c)Datanode d)secondary ...

  2. SQL Server优化相关的工具脚本

    SQL Server性能优化的一些常用脚本,适用于SQL Server 2008,更高的版本某些系统表的字段有所不同,建议参考MSDN. 死锁相关 /************************* ...

  3. rabbitMQ 笔记

    1. 端口 rabbitMQ  server 使用的端口是5672  ,   AMQP协议的端口 rabbitMQ  web 使用的端口是15672  ,   管理工具的端口 rabbitMQ  cl ...

  4. Turn Your Raspberry Pi Into a WiFi Hotspot with Edimax Nano USB EW-7811Un (RTL8188CUS chipset)

    http://www.daveconroy.com/turn-your-raspberry-pi-into-a-wifi-hotspot-with-edimax-nano-usb-ew-7811un- ...

  5. Code Understanding Step by Step - We Need a Task

      Code understanding is a task we are always doing, though we are not even aware that we're doing it ...

  6. maven依赖传递关系

    一.maven 依赖传递规则 举个例子,比如A依赖B,B依赖C,那么A也是依赖C的.A是对B的直接依赖,A对C是传递依赖 ①.最短路劲原则 如,路劲一:A依赖B,B依赖C,C依赖D(1.0.0): 路 ...

  7. PCL入门—点云操作 定义变量 显示点云 存储

    // 定义相关变量 pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_ptr (new pcl::PointCloud<pcl::PointXYZ& ...

  8. 在Struts2中使用ValueStack、ActionContext、ServletContext、request、session等 .

    笔者不知道该用哪个词来形容ValueStack.ActionContext等可以在Struts2中用来存放数据的类.这些类使用的范围不同,得到的方法也不同,下面就来一一介绍. 声明:本文参考Strut ...

  9. yum -------包安装库

    elrepo:   http://elrepo.org/tiki/tiki-index.php CentOSPlus:    http://wiki.centos.org/zh/AdditionalR ...

  10. 编程之linux与win区别

    换行符在Linux和Windows下的区别 一.区别 换行符: 1.windows中的换行符是\r\n, 2. linux/unix下的换行符是\n. 其中: 回车符:\r=0x0d (13) ret ...