查单词的:
create procedure FindRepeatWord(@inputstring varchar(1000),@repeatcount int)
as
set nocount on
declare @table table (id int identity(1,1),value varchar(1000))
declare @begin as varchar(20)
select @begin=''
declare @i as int
select @i=1
while @i<=len(@inputstring)
begin
if substring(@inputstring,@i,1)<>' '
select @begin=@begin+substring(@inputstring,@i,1)
else
begin
insert into @table values (@begin)
select @begin=''
end
select @i=@i+1
end
if len(@begin)<>0 insert into @table values (@begin)
select t1.value
from @table t1 join
(select value ,min(id) as minid from @table group by value ) t0
on t1.value=t0.value and t1.id<t0.minid+@repeatcount
group by t1.value
having count(*)=@repeatcount
set nocount off
if @@rowcount<>0
begin
print 'found'
return 1
end
else
begin
print 'not found'
return 0
end
有几项其实可以合并一下,用下面的存储过程(改写成函数也可以):
create procedure findrepeat(@inputstring varchar(1000),@repeatcount int)
as
declare @begin as varchar(20)
declare @i as int
select @i=1
select @begin=' '
while @i<=len(@inputstring)
begin
if substring(@inputstring,@i,1)=right(@begin ,1)
select @begin=@begin+substring(@inputstring,@i,1)
else
select @begin=substring(@inputstring,@i,1)
select @i=@i+1
if len(@begin)=@repeatcount begin print 'find' return 1 end
end