1、在同一类别下,根据当前文章ID查出小于当前文章的ID的最大文章ID。为上一篇。
2、在同一类别下,根据当前文章ID查出大于当前文章的ID的最小文章ID。为下一篇。
3、根据上一篇的文章ID,下一篇的文章ID,查出上一篇文章的标题,下一篇文章的标题。
4、最好把变量查出来,返回给程序。
create procedure [dbo].[spNews_S_NewsPrevNext]
@NewsID int,
@topid
int
as
declare @Prev int
declare @Next int
declare @PrevTitle
nvarchar(200)
declare @NextTitle nvarchar(200)
declare
@PrevCategoryContent nvarchar(200)
declare @NextCategoryContent
nvarchar(200)
set @Prev=(select max(id) from vNewsCategoryByNewsArticle where id<@NewsID
and topid=@topid and status=1)
set @Next=(select min(id) from
vNewsCategoryByNewsArticle where id>@NewsID and topid=@topid and
status=1)
if @Prev is not null
begin
set @PrevTitle=(select title
from vNewsCategoryByNewsArticle where id=@Prev and topid=@topid)
set
@PrevCategoryContent=(select topid from vNewsCategoryByNewsArticle where
id=@Prev and topid=@topid)
end
else
begin
set @Prev=0
set
@PrevCategoryContent=‘‘
set @PrevTitle=‘‘
end
if @Next is not
null
begin
set @NextTitle=(select Title from vNewsCategoryByNewsArticle
where id=@Next and topid=@topid)
set @NextCategoryContent=(select topid
from vNewsCategoryByNewsArticle where id=@Next and topid=@topid)
end
else
begin
set @Next=0
set @NextCategoryContent=‘‘
set
@NextTitle=‘‘
end
begin
select @Prev as Prev,@Next as
Next,@PrevTitle as PrevTitle,
@NextTitle as
NextTitle,
@PrevCategoryContent as
PrevCategoryContent,
@NextCategoryContent as
NextCategoryContent
end