前言:本人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. usb device selection

  2. 问题-在TreeView使用时,发现选中的树节点会闪烁或消失

    问题:在工程中选中一个树节点,鼠标焦点在树上,做某种操作时发现选中的点会消失?原因:如果只是BeginUpdate后,没有调用EndUpdate,树会全空.应该是BeginUpdate方法会刷新树,但 ...

  3. Latex 中宽度的设置和理解

    \textwidth, 文本区域的全部宽度 \columnwidth, 文本中一列的宽度,单栏或者多栏的情况下,值是不同的 但是,一旦\textwidth, \columnwidth, \linewi ...

  4. POJ 3130 How I Mathematician Wonder What You Are! /POJ 3335 Rotating Scoreboard 初涉半平面交

    题意:逆时针给出N个点,求这个多边形是否有核. 思路:半平面交求多边形是否有核.模板题. 定义: 多边形核:多边形的核可以只是一个点,一条直线,但大多数情况下是一个区域(如果是一个区域则必为 ).核内 ...

  5. IDF实验室-python ByteCode writeup

    题目地址:http://ctf.idf.cn/index.php?g=game&m=article&a=index&id=45 下载来发现是crackme.pyc 可以用unc ...

  6. <转>linux 下stm32开发环境安装

    传送门: http://www.eefocus.com/marianna/blog/13-10/298454_7e04f.html http://blog.sina.com.cn/s/blog_643 ...

  7. 看文章《EAI和SOA的比较》有感(1)

    <EAI和SOA的比较>http://www.cnblogs.com/asdling/archive/2007/11/26/973100.html这篇文章写的很全面,至少自己这么认为,也解 ...

  8. JAVA线程全局异常处理

    大家平时写线程很多,但可能很少关注如何捕获线程的全局异常.其实jdk提供了两种捕获全局异常的方法,一种是基于整个线程类(staticsetDefaultUnaughtExceptionHandler( ...

  9. android 处理网络状态——无网,2g,3g,wifi,ethernet,other

    今天在一位很牛逼的学长的博客里面看到了这段代码后,很是激动啊,于是就“偷”了过来,嘿嘿....为自己也为更多需要它的程序媛 直接上代码: public class GetNetWorkStateAct ...

  10. 02---CSS整理

    一.概述       CSS(cascading style sheet) 层叠样式表       提供比HTML更强大的页面排版.美化工具       CSS将网页内容和显示样式进行分离,提高了显示 ...