求个存储过程
存储过程名是:selectarticle(@page sbyte)
表article中有很多的记录,其中有个标识列articleID,我需要的存储过程能实现一下功能
当page为1时,按articleID逆序(从大到小)输出20条记录最低下的记录。
当page=2时,按articleID逆序输出最下面的40-20条记录。
当page=3时,按articleID逆序输出最下面的60-40条记录。
[解决办法]
- SQL code
create procedure selectarticle@a intasbegindeclare @b int--你的page参数set @a = 1--每次取出的条数if @a=1beginset @b = 20select top (@b-@a+1) * from tablea where ID not in (select top (@a-1) ID from tablea) order by ID descendif @a=2beginset @a=21set @b=40select top (@b-@a+1) * from tablea where ID not in (select top (@a-1) ID from tablea) order by ID descendif @a=3beginset @a=31set @b=60select top (@b-@a+1) * from tablea where ID not in (select top (@a-1) ID from tablea) order by ID descendend
[解决办法]
- SQL code
--将数据分次取出,每次取20条declare @a intdeclare @b intset @a = 1set @b = 20while @b<=(select count(*) from tt)begin select top (@b-@a+1) * from tt where ID not in (select top (@a-1) ID from tt)set @a = @b+1set @b = @b+20end
[解决办法]
- SQL code
create proc selectarticle(@page sbyte) asdeclare @s nvarchar(4000)if @page=1 select top 20 * from article order by articleID descelsebegin set @s='select top 20 * from article where iD not in( select top '+rtrim((@page-1)*20)+' from article order by articleID desc ) order by articleID desc') exec(@s)end
[解决办法]
执行动态sql