Code
Create Procedure dbo.News_SimilarNews
(
@topN varchar(10),
@id varchar(100),
@keywords nvarchar(500)
)
AS
SET NOCOUNT ON
declare @I int
declare @c int
set @c=0
declare @Keyword nvarchar(50)
declare @sql varchar(1000)
set @sql='select top('+@topN+') pp_id,pp_sortid,pp_title from [pp_information] where pp_id<>'+@id+' and ('
set @keywords=rtrim(ltrim(@keywords))
SET @I=CHARINDEX(' ', @Keywords)
while @I>0
begin
SET @Keyword=ltrim(rtrim(LEFT(@Keywords, @I-1)))
if @c=0
set @sql=@sql+'[pp_keywords] like ''%'+@Keyword+'%'''
else
begin
set @sql=@sql+' or [pp_keywords] like ''%'+@Keyword+'%'''
end
set @c=@c+1
SET @Keywords=SUBSTRING(@Keywords, @I+1,LEN(@Keywords)-@I)
SET @I=CHARINDEX(' ', @Keywords)
end
if @c>0
set @sql=@sql+' or [pp_keywords] like ''%'+@keywords+'%'''
else
begin
set @sql=@sql+'[pp_keywords] like ''%'+@keywords+'%'''
end
set @sql=@sql+') and pp_verify<>0 order by newid()'
Exec(@sql)
Create Procedure dbo.News_SimilarNews
(
@topN varchar(10),
@id varchar(100),
@keywords nvarchar(500)
)
AS
SET NOCOUNT ON
declare @I int
declare @c int
set @c=0
declare @Keyword nvarchar(50)
declare @sql varchar(1000)
set @sql='select top('+@topN+') pp_id,pp_sortid,pp_title from [pp_information] where pp_id<>'+@id+' and ('
set @keywords=rtrim(ltrim(@keywords))
SET @I=CHARINDEX(' ', @Keywords)
while @I>0
begin
SET @Keyword=ltrim(rtrim(LEFT(@Keywords, @I-1)))
if @c=0
set @sql=@sql+'[pp_keywords] like ''%'+@Keyword+'%'''
else
begin
set @sql=@sql+' or [pp_keywords] like ''%'+@Keyword+'%'''
end
set @c=@c+1
SET @Keywords=SUBSTRING(@Keywords, @I+1,LEN(@Keywords)-@I)
SET @I=CHARINDEX(' ', @Keywords)
end
if @c>0
set @sql=@sql+' or [pp_keywords] like ''%'+@keywords+'%'''
else
begin
set @sql=@sql+'[pp_keywords] like ''%'+@keywords+'%'''
end
set @sql=@sql+') and pp_verify<>0 order by newid()'
Exec(@sql)
或全文索引