sql 解析字符串添加到临时表中  sql存储过程in 参数输入 解决方法

把字符串解析 添加到 临时表中

SELECT * into #临时表   FROM dbo.Func_SplitOneColumnTabel('001,gf', ',')

select * from 表 where ID in (select id from 临时表)

Create function [dbo].[Func_SplitOneColumnTabel]
(@str nvarchar(max),@split varchar(10))
returns @t Table (keyColumn varchar(200))
as
begin
            while len(@str)>0
            begin
                if CHARINDEX(@split,@str)>0           
                    begin
                        INSERT INTO @t(keyColumn) VALUES(LEFT(@str,CHARINDEX(@split,@str)-1))                    
                        set @str=RIGHT(@str,len(@str)-CHARINDEX(@split,@str))   
                    end
                else
                    begin
                        INSERT INTO @t(keyColumn) VALUES(@str)
                        set @str=''
                    end
            end
    return
end

使用方法

SELECT *   FROM dbo.Func_SplitOneColumnTabel('001,gf', ',')

结果

keyColumn
001
gf