将逗号分隔的varchar参数拆分为临时表

时间:2015-04-07 10:10:25

标签: sql sql-server

我有以下存储过程:

CREATE PROCEDURE myProc
    @nameList varchar(500)
AS
BEGIN
    create table #names (Name varchar(20))

    -- split @nameList up into #names table
END
GO

@nameList基本上会是这样的:

'John, Samantha, Bob, Tom'

4 个答案:

答案 0 :(得分:4)

使用转换为XMLcross apply

  DECLARE @str varchar(50)
  SET @str='John, Samantha, Bob, Tom'

  SELECT names = y.i.value('(./text())[1]', 'nvarchar(1000)')             
  FROM 
  ( 
    SELECT 
        n = CONVERT(XML, '<i>' 
            + REPLACE(@str, ',' , '</i><i>') 
            + '</i>')
  ) AS a 
  CROSS APPLY n.nodes('i') AS y(i)

输出:

names
-----
John
 Samantha
 Bob
 Tom

编辑:它不需要proc中的临时表,所以proc将是:

CREATE PROCEDURE myProc

    (@nameList varchar(500))

AS
BEGIN

      SELECT names = y.i.value('(./text())[1]', 'nvarchar(1000)')             
      FROM 
      ( 
        SELECT 
            n = CONVERT(XML, '<i>' 
                + REPLACE(@nameList, ',' , '</i><i>') 
                + '</i>')
      ) AS a 
      CROSS APPLY n.nodes('i') AS y(i)
END

但如果要将其插入临时表,则下面是示例:

create table #names 
    (
        Name varchar(20)
    )

  DECLARE @str varchar(50)
  SET @str='John, Samantha, Bob, Tom'

  insert into #names
  SELECT names = y.i.value('(./text())[1]', 'nvarchar(1000)')             
  FROM 
  ( 
    SELECT 
        n = CONVERT(XML, '<i>' 
            + REPLACE(@str, ',' , '</i><i>') 
            + '</i>')
  ) AS a 
  CROSS APPLY n.nodes('i') AS y(i)

  select * from #names 
  drop table #names 

编辑2:输入字符串可能包含一些特殊字符,例如'<' , '>' , etc它不是名称的标准,但如果给定的字符串包含它们,您可以使用它们删除它们replace函数:replace(@str,'<','')

答案 1 :(得分:1)

使用递归cte:

DECLARE @nameList NVARCHAR(MAX) = 'John, Samantha, Bob, Tom'
SET @nameList = @nameList + ',';

WITH    cte
          AS ( SELECT   SUBSTRING(@nameList, 0, CHARINDEX(',', @nameList)) AS n ,
                        CHARINDEX(',', @nameList) AS i
               UNION ALL
               SELECT   SUBSTRING(@nameList, i + 2,CHARINDEX(',', @nameList, i + 2) - i - 2) ,
                        CHARINDEX(',', @nameList, i + 2)
               FROM     cte
               WHERE    CHARINDEX(',', @nameList, i + 2) > 0
             )
    SELECT  n FROM    cte

输出:

n
John
Samantha
Bob
Tom

答案 2 :(得分:0)

您可以创建功能并从您想要分割的地方调用此功能: -

create FUNCTION [dbo].[SplitStrings](@nameList varchar(MAX), @Delimiter char(1))       
returns @temptable TABLE (names varchar(MAX))       
as       
begin      
    declare @id int       
    declare @x varchar(8000)       

    select @id = 1       
        if len(@nameList)<1 or @nameList is null  return       

    while @id!= 0       
    begin       
        set @id = charindex(@Delimiter,@nameList)       
        if @id!=0       
            set @x = left(@nameList,@id - 1)       
        else       
            set @x = @nameList

        if(len(@x)>0)  
            insert into @temptable(names) values(@x)       

        set @String = right(@nameList,len(@nameList) - @id)       
        if len(@nameList) = 0 break       
    end   
return 
end;

答案 3 :(得分:0)

CREATE FUNCTION [dbo].[Split]
(
    @RowData nvarchar(MAX),
    @SplitOn nvarchar(5)
)  
RETURNS @RtnValue table 
(
    Id int identity(1,1),
    Data nvarchar(100)
) 
AS  
BEGIN 
    Declare @Cnt int
    Set @Cnt = 1

    While (Charindex(@SplitOn,@RowData)>0)
    Begin
        Insert Into @RtnValue (data)
        Select 
            Data = ltrim(rtrim(Substring(@RowData,1,Charindex(@SplitOn,@RowData)-1)))

        Set @RowData = Substring(@RowData,Charindex(@SplitOn,@RowData)+1,len(@RowData))
        Set @Cnt = @Cnt + 1
    End

    Insert Into @RtnValue (data)
    Select Data = ltrim(rtrim(@RowData))

    Return
END