create procedure show_coursepage
(
 @rowcount smallint,   --总记录数
 @pageindex smallint,  --页面序号
 @pagesize  smallint,  --页面大小
 @wherelimit varchar(500)='',   --where条件限制(注意:含'where')
 @orderfield varchar(300),         --排序依据(注:不含order by 只能提供1个字段名)
 @orderdirection tinyint --排序顺序,0为降序,1为升序  
  
)
as
set nocount on
declare @mainsql varchar(5000)  --主sql语句
declare @maindirection varchar(5)  --主排序
declare @todirection varchar(5) --反序
declare @orderby varchar(300)
declare @newpagesize smallint   --新的页面大小,因最后一页记录数有可能<>pagesize
declare @topcount smallint  --top记录数量
declare @pagecount smallint --总页面数
declare @temporder varchar(100) --临时变量
declare @lastorder varchar(200) --最后一个排序命令,若是前半数页,必须,否则为空

set @maindirection='desc' --默认为降序
set @todirection='asc'

if @orderdirection>0           --如果是升序,重新赋值
begin
 set @maindirection='asc'
 set @todirection='desc'
end

set @pagecount=@rowcount/@pagesize    --得到总页面数
if @rowcount%@pagesize>0  --如果有余数,最后页不是整页,总页数需加一
set @pagecount=@pagecount+1

set @lastorder=''  --若是最后一页,则为空

set @mainsql='select * from (select top '
-----------------------------------------------------------------------------------------------------------
if @pageindex<=@pagecount/2  --如果请求页面位于前一半页面,则select top pagesize*pageindex就可以
begin
   set @newpagesize=@pagesize   --非最后一页
   set @topcount=@pageindex*@pagesize  --top的记录数量
   set @lastorder='order by '+cast(@orderfield as varchar(300))+' '+@maindirection --最后一个排序依据,前半段页面,此处必须填充
  
end
------------------------------------------------------------------------------------------------------------
else   --请求页面位于后一半页面,还要分是否>总页面数,若然,则请求最后一页,最后一页的@topcount=@newpagesize=@rowcount%@pagesize
begin  --@topcount为第一层,@newpagesize为第二层。若为末页,
 
 set @temporder=@maindirection
 set @maindirection=@todirection  --交换排序方向
 set @todirection=@temporder

  if @pageindex>=@pagecount --请求页为末页
    begin  
       if @rowcount%@pagesize=0 --若为整页的情况,topcount=pagesize
         begin
             set @topcount=@pagesize  
             set @newpagesize=@pagesize
         end
       else
         begin
             set @topcount=@rowcount%@pagesize
             set @newpagesize=@topcount
         end
    end
 else    -----请求页为非末页
   begin
      set @topcount=@rowcount-(@pageindex-1)*@pagesize
      set @newpagesize=@pagesize
   end
     
end
set @mainsql=@mainsql+cast(@newpagesize as varchar(10))+' * from  (select top '+  cast(@topcount as varchar(10))
set @mainsql=@mainsql+' zonename  ,clistno,cname,module,langno,credithour,ctexttime '+
' from courselist  join courseinfo  on courselist.cno=courseinfo.cno '+
' join coursemodule  on courseinfo.moduleno=coursemodule.moduleno join schoolzone on courselist.zno=schoolzone.zno  '

if len(@wherelimit)>0
set @mainsql=@mainsql+'  '+@wherelimit+'  '
set @mainsql=@mainsql+' order by  '+cast(@orderfield as varchar(300))+'  '+'  '+@maindirection+'  ) as aa '+
'order by  '+cast(@orderfield as varchar(300))+'  '+'  '+@todirection+'  ) as bb '+
cast(@lastorder as varchar(500))
--print @mainsql
execute(@mainsql)
set nocount off