--split函数,其作用是将字符串拆分成表CREATE FUNCTION [fn_split](@SourceSql varchar(8000), @StrSeprate varchar(10))RETURNS @temp table( [n] int NULL, [a] varchar(100) NULL)ASBEGIN declare @i int,@n int; set @n=0; set @SourceSql=rtrim(ltrim(@SourceSql)); set @i=charindex(@StrSeprate,@SourceSql); while (@i>=1) begin set @n=@n+1; insert @temp([n],[a]) values(@n,left(@SourceSql,@i-1)); set @SourceSql=substring(@SourceSql,@i+1,len(@SourceSql)-@i); set @i=charindex(@StrSeprate,@SourceSql); end if (@SourceSql<>'') begin set @n=@n+1; insert @temp([n],[a]) values(@n,@SourceSql); end returnENDGO--函数的使用:select * from fn_split('AA,BB,CC,DD',',')结果如下:
追问别乱复制过来