create procedure test
@id int
as declare @previd int ,@nextid int
select @previd=max(id) from testft where id<@id
select @nextid=min(id) from testft where id>@idset @previd=isnull(@previd,0)
set @nextid=isnull(@nextid,0)select id,title,content,previd=@previd,nextid=@nextid from  tb_news where id=@id

解决方案 »

  1.   

    create proc p_qry
    @id int,         --要查询的id
    @preid int out,  --前一条的id
    @nextid int out, --后一条的id
    as
    select @preid=isnull(max(id),0) from tb_news where id<@id
    select @nextid=isnull(min(id),0) from tb_news where id>@id
    select * from tb_news where id=@id
    go
    --调用示例
    declare @preid int,@nextid int
    exec p_qry 1,@preid out,@nextid out
      

  2.   

    CREATE proc p_qry
    @id int        --要查询的id
    as
    declare
    @preid int ,  --前一条的id
    @nextid int  --后一条的id
    select @preid=isnull(max(id),0) from aa where id<@id
    select @nextid=isnull(min(id),0) from aa where id>@id
    select *,@preid as Pre,@nextid as Next   from aa where id=@id
    GO
      

  3.   

    select id,title,content,select max(id) from testft where id<@id as @previd,select min(id) from tb_news where id>@id as nextid
    from tb_news where id=@id