SQL字符串,根据逗号拆成多条记录

–select * from dbo.[fn_get_1](‘12,12,12|as,adsf,df|’)

CREATE FUNCTION [dbo].[fn_get_1](@str varchar(8000))
returns @tb table(ids int,col varchar(5000))
as
begin
if (right(@str,1)!=’|’)
begin
set @str=@str+’|’
end

declare @a int 
set @a=1

while charindex('|',@str)>0
begin
    insert into @tb(ids,col)
    select @a,left(@str,charindex('|',@str)-1)

    set @str=right(@str,len(@str)-charindex('|',@str))
    set @a=@a+1
end 
return   

end

你可能感兴趣的:(sql,server)