日期:2014-05-18 浏览次数:20607 次
create procedure selectarticle @a int as begin declare @b int --你的page参数 set @a = 1 --每次取出的条数 if @a=1 begin set @b = 20 select top (@b-@a+1) * from tablea where ID not in (select top (@a-1) ID from tablea) order by ID desc end if @a=2 begin set @a=21 set @b=40 select top (@b-@a+1) * from tablea where ID not in (select top (@a-1) ID from tablea) order by ID desc end if @a=3 begin set @a=31 set @b=60 select top (@b-@a+1) * from tablea where ID not in (select top (@a-1) ID from tablea) order by ID desc end end
------解决方案--------------------
--将数据分次取出,每次取20条 declare @a int declare @b int set @a = 1 set @b = 20 while @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+1 set @b = @b+20 end
------解决方案--------------------
create proc selectarticle(@page sbyte) as declare @s nvarchar(4000) if @page=1 select top 20 * from article order by articleID desc else begin 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