日期:2014-05-18  浏览次数:20607 次

求个存储过程
存储过程名是: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 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

------解决方案--------------------
SQL code

--将数据分次取出,每次取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

------解决方案--------------------
SQL code
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