ALTER FUNCTION  [dbo].[UFun_SqlSplit] (@Str varchar(120),@Sp varchar(120))
RETURNs @table TABLE(       
ID int IDENTITY PRIMARY KEY,     
value nvarchar(max)   
)
BEGIN
/*
Test:
select * from SqlSplit('123,234',',')
*/
IF LEN(@Str)<1
return;

 DECLARE @pos int
 DECLARE @temppos int
 SET @pos =0;
 WHILE(CHARINDEX(@Sp,@Str,0)>0)
BEGIN
SET @temppos =CHARINDEX(@Sp,@Str,0)
INSERT INTO @table VALUES(substring(@Str,1,@temppos-1))
SET @Str=substring(@Str,@temppos+1,len(@Str))

END
INSERT INTO @table VALUES(@Str)
RETURN
END

posted on 2012-05-04 10:01  万德源  阅读(275)  评论(0编辑  收藏  举报