将字符串拆分为id
Sql :
alter function [fn_splitSTR] ( @s varchar(8000), -- 5,6,7 @split varchar(10) -- ',' ) return @re table(col varchar(100)) as begin declare @splitlen int set @splitlen=len(@split+'a ')-2 while charindex(@split,@s)>0 begin insert @re values(left(@s,charindex(@split,@s)-1)) set @s=stuff(@s,1,charindex(@split,@s)+ @splitlen,' ') end insert @re values(@s) return end
执行:
select*from dbo.fn_splitSTR('5,6,7',',')
结果: