将值存储在SQL中的不同变量中,由(逗号)“,”分隔

时间:2017-01-20 09:40:19

标签: mysql sql sql-server database sql-server-2008

我需要分离值并将它们存储在SQL中的不同变量中,

例如

a='3100,3101,3102,....'

输出应为

x=3100
y=3101
z=3102
.
.
.

3 个答案:

答案 0 :(得分:1)

    create function [dbo].[udf_splitstring] (@tokens    varchar(max),
                                         @delimiter varchar(5))
returns @split table (
  token varchar(200) not null )
as
  begin
      declare @list xml

      select @list = cast('<a>'
                          + replace(@tokens, @delimiter, '</a><a>')
                          + '</a>' as xml)

      insert into @split
                  (token)
      select ltrim(t.value('.', 'varchar(200)')) as data
      from   @list.nodes('/a') as x(t)

      return
  end

GO







declare @cad    varchar(100)='3100,3101,3102'
    select  *,ROW_NUMBER() over (order by token ) as rn    from udf_splitstring(@cad,',')


token   rn
3100    1
3101    2
3102    3

答案 1 :(得分:1)

Parse TVF的结果可以很容易地合并到JOIN或IN

Declare @a varchar(max)='3100,3101,3102'
Select * from [dbo].[udf-Str-Parse](@a,',')

返回

RetSeq  RetVal
1       3100
2       3101
3       3102

UDF如果需要(比递归,循环和xml快得多)

CREATE FUNCTION [dbo].[udf-Str-Parse] (@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','||')

答案 2 :(得分:0)

我建议您使用以下查询,它比其他函数(例如cross apply和udf)快得多。

SELECT 
    Variables
    ,S_DATA
FROM    (
    SELECT
            Variables
            ,CASE   WHEN LEN(LIST2)>0 THEN LTRIM(RTRIM(SUBSTRING(LIST2, NUMBER+1, CHARINDEX(',', LIST2, NUMBER+1)-NUMBER - 1)))
                    ELSE NULL
            END AS S_DATA 
           ,NUMBER
    FROM(
        SELECT  Variables
                ,','+COMMA_SEPARETED_COLUMN+',' LIST2
                FROM Tb1
        )DT 
         LEFT OUTER JOIN TB N ON (N.NUMBER < LEN(DT.LIST2)) OR (N.NUMBER=1 AND DT.LIST2 IS NULL)
        WHERE SUBSTRING(LIST2, NUMBER, 1) = ',' OR LIST2 IS NULL
     ) DT2
     WHERE S_DATA<>''

你也应该创建一个表格&#39; NUMBER&#39;在运行上述查询之前。

CREATE TABLE TB (Number INT)
 DECLARE @I INT=0
 WHILE @I<1000
 BEGIN
 INSERT INTO TB VALUES (@I)
 SET @I=@I+1
 END