前言:本人SQL技术很烂,然后工作时间也不久,许多东西都还在学习中,说的不好的地方尽请谅解.

首先跟大家说一下我今天遇到的问题吧.

SQL-学习使用FOR XML PATH

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

SQL-学习使用FOR XML PATH

首先大家可以看到.第一张图的Res_Data中有多个部门的GUID,中间用逗号隔开的.

我当时想到的愚蠢的办法就是

 1 @MaterialTypeName nvarchar(200),
 2 @CentralizedName nvarchar(200),
 3 @start int,
 4 @limit int,
 5 @totalCount int output 
 6 AS
 7 BEGIN
 8     SET NOCOUNT ON;
 9     select 
10         ROW_NUMBER() over (order by res_id asc) as RowNumber,
11         * 
12     into #List 
13     from 
14         UBIPlatform..T_RESOURCE 
15     WHERE Res_Parent_Code='741cdd2bef2e479f8c5dd35cf6e8bf2a'
16      
17         
18     declare @i int,@count int;
19     declare @Centralized nvarchar(200);
20     declare @List1 table(id int, ResId nvarchar(50),ResName nvarchar(50),ResData nvarchar(50));
21     select @count=COUNT(*) from UBIPlatform..T_RESOURCE WHERE Res_Parent_Code='741cdd2bef2e479f8c5dd35cf6e8bf2a' 
22     set @i=1
23     while @i<=@count
24     begin
25         if @i in (select RowNumber from #List)
26         begin
27             set @Centralized='';
28             select 
29                 @Centralized=@Centralized+','+LTRIM(Res_Name) 
30             from UBIPlatform.dbo.FN_GETMultiValTable(
31                 (select 
32                     Res_Data 
33                 from 
34                     UBIPlatform..T_RESOURCE 
35                 where 
36                     Res_Id=(select Res_Id from #List where RowNumber=@i))) ge
37             inner join UBIPlatform..T_RESOURCE r on r.Res_Id=ge.nvalue
38             
39             if @Centralized!=''
40             begin
41                 insert into 
42                     @List1 
43                 select 
44                     @i,
45                     Res_Id,
46                     Res_Name,
47                     (RIGHT(@Centralized,LEN(@Centralized)-1)) 
48                 from 
49                     UBIPlatform..T_RESOURCE 
50                 where Res_Id=(select Res_Id from #List where RowNumber=@i)
51             end
52             else
53             begin
54                 insert into 
55                     @List1 
56                 select 
57                     @i,
58                     Res_Id,
59                     Res_Name,
60                     @Centralized
61                 from 
62                     UBIPlatform..T_RESOURCE 
63                 where Res_Id=(select Res_Id from #List where RowNumber=@i)
64             end    
65         end
66         set @i=@i+1
67     end
68     
69     select ROW_NUMBER() over (order by id asc) as RowNumber,* into #List2 from @List1 where 
70     (@MaterialTypeName is null or @MaterialTypeName = '' or ResName  like '%'+@MaterialTypeName+'%')
71     and (@CentralizedName is null or @CentralizedName = '' or ResData  like '%'+@CentralizedName+'%')
72     
73     select top(@limit) * from #List2 where RowNumber > @start order by RowNumber asc
74     select @totalCount =COUNT(1) from @List1
75 END
View Code

相关文章:

  • 2021-06-28
  • 2022-12-23
  • 2021-08-15
  • 2022-12-23
  • 2022-02-26
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-08-04
  • 2021-08-22
  • 2022-01-05
相关资源
相似解决方案