Create Function [dbo].[Func_SplitStrToTable](@str varchar(8000))
Returns @tableName Table
(
str2table varchar(100)
)
As
Begin
set @str = @str+','
Declare @insertStr varchar(100)
Declare @newstr varchar(8000)
set @insertStr = left(@str,charindex(',',@str)-1)
set @newstr = stuff(@str,1,charindex(',',@str),'')
Insert @tableName Values(@insertStr)
while(len(@newstr)>0)
begin
set @insertStr = left(@newstr,charindex(',',@newstr)-1)
Insert @tableName Values(@insertStr)
set @newstr = stuff(@newstr,1,charindex(',',@newstr),'')
end
Return
End
调用方法:
select Func_SplitStrToTable('1,2,3,4,5')