新闻上一条下一条存储过程
今天晚上写了个上下条新闻存储程序
declare @id int
set @id=1
if exists(select * from tbl1 where id<@id) and exists(select * from tbl1 where id>@id)
select * from (select top 1 id,title from tbl1 where id<@id order by id desc)t1
union
select * from (select top 1 id,title from tbl1 where id>@id order by id asc)t2
else if not exists(select * from tbl1 where id<@id) and exists(select * from tbl1 where id>@id)
select id=@id-1, title=null
union
select * from (select top 1 id,title from tbl1 where id>@id order by id asc)t2
else if exists(select * from tbl1 where id<@id) and not exists(select * from tbl1 where id>@id)
select * from (select top 1 id,title from tbl1 where id<@id order by id desc)t2
union
select id=@id+1, title=null
else
select id=@id+1, title=null
浙公网安备 33010602011771号