如何分离出GUID以执行连接

时间:2016-08-17 13:17:30

标签: sql sql-server reporting-services guid

当存在大量形成参数的GUID时,我在SSRS中输出:

  

FinancialYear = 47faabe1-f6aa-e411-941e-00155d010d10&安培;
  FinancialPeriod = 4dfaabe1-f6aa-e411-941e-00155d010d10&安培;
  分支= eefb631b-4e1b-e611-9436-00155d010d10&安培;
  分支= 369d0919-edaa-e411-941e-00155d010d10&安培;
  分支= e9a7dc75-f9aa-e411-941e-00155d010d10&安培;
  分支= cb3a3c67-f9aa-e411-941e-00155d010d10&安培;
  部= 00000000-0000-0000-0000-000000000000和放大器;
  部= adb0f49a-4f1b-e611-9436-00155d010d10和放大器;
  部= 487c64ae-4f1b-e611-9436-00155d010d10和放大器;
  部门= 4c9b0919-edaa-e411-941e-00155d010d10

有没有人知道将这些分开的方法,以便进行联接并获取他们的名字?

理想情况下,输出看起来像:

FinancialYear 2016
FinancialPeriod 12
Branch London
Branch Main
Branch Manchester
Branch Leeds
Department Mortgages
Department Loans
Department Insurance
Department Life Assurance

1 个答案:

答案 0 :(得分:0)

借助解析器

Seq Item            Value
1   FinancialYear   47faabe1-f6aa-e411-941e-00155d010d10
2   FinancialPeriod 4dfaabe1-f6aa-e411-941e-00155d010d10
3   Branch          eefb631b-4e1b-e611-9436-00155d010d10
4   Branch          369d0919-edaa-e411-941e-00155d010d10
5   Branch          e9a7dc75-f9aa-e411-941e-00155d010d10
6   Branch          cb3a3c67-f9aa-e411-941e-00155d010d10
7   Department      00000000-0000-0000-0000-000000000000
8   Department      adb0f49a-4f1b-e611-9436-00155d010d10
9   Department      487c64ae-4f1b-e611-9436-00155d010d10
10  Department      4c9b0919-edaa-e411-941e-00155d010d10

返回

CREATE FUNCTION [dbo].[udf-Str-Parse] (@String varchar(max),@Delimeter varchar(10))
--Usage: Select * from [dbo].[udf-Str-Parse]('Dog,Cat,House,Car',',')
--       Select * from [dbo].[udf-Str-Parse]('John Cappelletti was here',' ')

Returns @ReturnTable Table (Key_PS int IDENTITY(1,1), Key_Value varchar(max))
As
Begin
   Declare @XML xml;Set @XML = Cast('<x>' + Replace(@String,@Delimeter,'</x><x>')+'</x>' as XML)
   Insert Into @ReturnTable Select ltrim(rtrim(String.value('.', 'varchar(max)'))) FROM @XML.nodes('x') as T(String)
   Return 
End

UDF(如果需要)

Declare @executionLog table (ID int, parameters varchar(max))
Insert into @executionLog values
(1,'FinancialYear=47faabe1-f6aa-e411-941e-00155d010d10&FinancialPeriod=4dfaabe1-f6aa-e411-941e-00155d010d10&Branch=eefb631b-4e1b-e611-9436-00155d010d10&Branch=369d0919-edaa-e411-941e-00155d010d10&Branch=e9a7dc75-f9aa-e411-941e-00155d010d10&Branch=cb3a3c67-f9aa-e411-941e-00155d010d10&Department=00000000-0000-0000-0000-000000000000&Department=adb0f49a-4f1b-e611-9436-00155d010d10&Department=487c64ae-4f1b-e611-9436-00155d010d10&Department=4c9b0919-edaa-e411-941e-00155d010d10'),
(2,'FinancialYear=someGUID&FinancialPeriod=anotherGUID')

Select A.*
      ,B.*
 From  @executionLog A
 Cross Apply (
                Select Seq   =Key_PS
                      ,Item  =Substring(Key_Value,1,charindex('=',Key_Value)-1)
                      ,Value =Substring(Key_Value,  charindex('=',Key_Value)+1,100)
                 From [dbo].[udf-Str-Parse](parameters,'&')
             ) B
  

编辑 - 以下说明如果您需要生成多对多

,请如何使用Cross Apply
war