Want a stored procedure for comma seperated string which is of a column (has 20000 rows ) in a table

前端 未结 1 1325
太阳男子
太阳男子 2020-12-12 03:53

I have a table with 20000 rows, and a column that has comma-separated strings in it. I want split each of the comma-separated strings from each row and insert them into a te

相关标签:
1条回答
  • 2020-12-12 04:24

    A Cross Apply with a a Parse should do the trick

    Option 1 - With a UDF

    Select A.ID
          ,B.* 
      From Campaign A
      Cross Apply [dbo].[tvf-Str-Parse-8K](A.Misc,',') B
    

    Option 2 - Without a UDF

    Select A.ID
          ,B.* 
      From Campaign A
      Cross Apply (
                    Select RetSeq = Row_Number() over (Order By (Select null))
                          ,RetVal = LTrim(RTrim(B.i.value('(./text())[1]', 'varchar(max)')))
                    From  (Select x = Cast('<x>' + replace((Select replace(A.Misc,',','§§Split§§') as [*] For XML Path('')),'§§Split§§','</x><x>')+'</x>' as xml).query('.')) as A 
                    Cross Apply x.nodes('x') AS B(i)
                  ) B
    

    Both Return

    ID  RetSeq RetVal
    1   1       h101
    1   2       h202
    1   3       h506
    1   4       h707
    1   5       h112
    1   6       h566
    2   1       h101
    2   2       h102
    2   3       h508
    ...
    4   3       h432
    4   4       h456
    4   5       h678
    4   6       h101
    4   7       h807
    

    The UDF if Needed

    CREATE FUNCTION [dbo].[tvf-Str-Parse-8K] (@String varchar(max),@Delimiter varchar(25))
    Returns Table 
    As
    Return (  
        with   cte1(N)   As (Select 1 From (Values(1),(1),(1),(1),(1),(1),(1),(1),(1),(1)) N(N)),
               cte2(N)   As (Select Top (IsNull(DataLength(@String),0)) Row_Number() over (Order By (Select NULL)) From (Select N=1 From cte1 a,cte1 b,cte1 c,cte1 d) A ),
               cte3(N)   As (Select 1 Union All Select t.N+DataLength(@Delimiter) From cte2 t Where Substring(@String,t.N,DataLength(@Delimiter)) = @Delimiter),
               cte4(N,L) As (Select S.N,IsNull(NullIf(CharIndex(@Delimiter,@String,s.N),0)-S.N,8000) From cte3 S)
    
        Select RetSeq = Row_Number() over (Order By A.N)
              ,RetVal = LTrim(RTrim(Substring(@String, A.N, A.L)))
        From   cte4 A
    );
    --Orginal Source http://www.sqlservercentral.com/articles/Tally+Table/72993/
    --Much faster than str-Parse, but limited to 8K
    --Select * from [dbo].[udf-Str-Parse-8K]('Dog,Cat,House,Car',',')
    --Select * from [dbo].[udf-Str-Parse-8K]('John||Cappelletti||was||here','||')
    --Performance On a 5,000 random sample -8K 77.8ms, -1M 79ms (+1.16), -- 91.66ms (+13.8)
    
    0 讨论(0)
提交回复
热议问题