CREATE function [dbo].[f_split](@SourceSql varchar(8000),@StrSeprate varchar(10))
returns @temp table(id varchar(100))
--实现split功能 的函数
as
begin
declare @i int
set @SourceSql=rtrim(ltrim(@SourceSql))
set @i=charindex(@StrSeprate,@SourceSql)
while @i>=1
begin
insert @temp values(left(@SourceSql,@i-1))
set @SourceSql=substring(@SourceSql,@i+1,len(@SourceSql)-@i)
set @i=charindex(@StrSeprate,@SourceSql)
end
if @SourceSql<>'\'
insert @temp values(@SourceSql)
return
end
create function [dbo].[f_split1](@c varchar(2000),@split varchar(2))
returns @t table(col varchar(20))
as
begin
while(charindex(@split,@c)<>0)
begin
insert @t(col) values (substring(@c,1,charindex(@split,@c)-1))
set @c = stuff(@c,1,charindex(@split,@c),'')
end
insert @t(col) values (@c)
return
end
DECLARE @count int
DECLARE @c int
DECLARE @r int
SET @c= 0
SELECT @count =count(*) FROM f_split1('1,2,3',',')
WHILE @c< @count
BEGIN
set @c=@c+1
--SELECT @r=col FROM f_split1('1,2,3',',') WHERE
--SELECT col,(ROW_NUMBER() OVER(ORDER BY col asc)) AS RowNum FROM f_split1('1,2,3',',') WHERE RowNum=1
select @r=col from
(select Row_Number() Over(order by col desc) as RowNum, *
from f_split1('7,5,6',',')) a
where RowNum=@c
PRINT @r
END