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 ...
随机推荐
- IOS上的字符编码
IOS字符编码 字符编码经常能听到的有Unicode.UTF-8.GBK.GB2312.GB18030.BIG5等.这些编码方式都有什么区别呢? 其实这些编码,大体分两类:全球统一编码和各过个语种的独 ...
- <转>HTML中的table转为excel
转换html 中的table 为excel,firefox浏览器支持,代码如下 <%@ page language="java" contentType="text ...
- Keil: warning: A1581W: Added 2 bytes of padding at address
KEIL MDK编译警告: warning: A1581W: Added 2 bytes of padding at address xxx 原因: 在Keil 里写汇编代码时如果代码尺寸不对齐, ...
- 让Visual Studio 2015 支持ASP.NET MVC4.0.0.1
近日装上了Visual Studio 2015 ,打开之前vs2013创建的MVC4的项目发现无法编译通过,提示System.Web.MVC,System.Web.WebPages 等找不到,网上搜索 ...
- cocos2d-x 手势之简单实现
转自:http://blog.sina.com.cn/s/blog_61ece099010187tl.html 手势之前也发过一篇,但是我感觉那个还不够轻巧. 而且大多数游戏里面不会有那么复杂的手势, ...
- 通过VMName获取VM IP
PS3.0下通过测试,PS2.0下没有 networkAdapters 这个属性: $vmname = "22012r2" $v = get-vm |where {$_.name ...
- oracle查询前10条记录
select * from table_name where rownum<11;
- ios开发——笔记篇
:开关 BOOL isopen = !isopen; //View @property (nonatomic, assign) BOOL open;//模型属性 self.group.open = ! ...
- android132 360 05 手机定位
.网络定位:根据ip地址定位,根据ip地址在实际地址数据库中查询实际地址. 缺点:动态ip导致地址不准确. .基站定位:3个基站就可以确定实际位置,定位范围是几百米到几公里不等. .GPS定位:美国卫 ...
- C _数据结构 _线性表的顺序存储
#ifndef __MY_SEQLIST_H__ #define __MY_SEQLIST_H__ typedef void SeqList; typedef void SeqListNode; // ...