/*
功能描述:对指定满足条件的数据库表进行分页查询 
作    者:saiko(引用请保留此信息) 
版    本:1.10
最后修改:2005-05-24            
调用例子:
  EXEC spoSplitPages  '你的SQL语句','排序字段',0 ,30, 1
           -- 0: 最后一页  30:每页记录数  1:降序说    明:A.支持多个表联合查询
  B.输出总记录数
  C.输出总页数
          通过使用MsSql未公开的扩展过程sp_ddopen来实现,速度快,简单易用 
*/
CREATE  procedure spoSplitPages
@SqlQuery nvarchar(4000), --查询字符串
@OrderFieldName  varchar(30),  --按该列为关键字来进行排序分页
@CurrentPage int,--第N页 (如果是 0  则读取 最后 一页的记录  )
@PageSize int,--每页行数
@OrderType    int        --排序, 0:顺序,  1:  其它数字: 不进行排序 (只有此处的数字不为 0 为 1 的时候,外程序传入的  OrderFieldName  才可以为空!)asDECLARE @sql nvarchar(4000), @tPageCount int IF @OrderType=0
  SET @sql= @SqlQuery + '  ORDER BY  '+@OrderFieldName IF @OrderType=1
  SET @sql =@SqlQuery + '  ORDER BY  '+@OrderFieldName + '  DESC '
    else
SET @sql =@SqlQuery
BEGIN
     set nocount on
declare @P1 int,--P1是游标的id
@rowcount int
--print  @sql
exec sp_cursoropen @P1 output,@sql,@scrollopt=1,@ccopt=1,@rowcount=@rowcount output
SET  @tPageCount = ceiling(1.0*@rowcount/@PageSize)
if @CurrentPage = 0 
set  @CurrentPage = @tPageCount-- @PageIndex = 0 表示在调用时,是首次查询,先读取 最后 一页的记录  
set @CurrentPage=(@CurrentPage-1)*@PageSize+1
exec sp_cursorfetch @P1,16,@CurrentPage,@PageSize 
exec sp_cursorclose @P1select  @tPageCount as PagesCount ,@rowcount as RowsCount,@CurrentPage as CurrentPage  
    set nocount off
END
GO

解决方案 »

  1.   

    嗯,收藏,谢谢楼上的,
    后来我把邹老大的和pbsql版主的结合拼出一个,可以实现我的要求了
    CREATE Proc pages
    @TableName nvarchar(4000),--表名,视图名,查询语名
    @PrimaryKey nvarchar(250),--主键名
    @PageSize int=10,--每页的大小(行数)
    @PageCurrent int=1,--要显示的页@ShowList nvarchar (4000)='', --要显示的字段列表,如果查询结果有标识字段,需要指定此值,且不包含标识字段
    @OrderCol nvarchar (1000)='' ,--排序字段列表
    @OrderType bit,           --排序 0-顺序,1-倒序
    @where nvarchar(1000) --查询条件
    as
    declare @Id1 varchar(20),@Id2 varchar(20),--开始和结束的记录号
    @where1 nvarchar(1000), --not in里边的查询条件
    @where2 nvarchar(1000)--外面的查询条件 select @Id1=cast(@PageSize as varchar(20))
      ,@Id2=cast((@PageCurrent-1)*@PageSize as varchar(20))--IF @where is null or rtrim(@where)='' select @ShowList=case isnull(@ShowList,'') when '' then ' *' else ' '+@ShowList end
    ,@where1=case isnull(@where,'')when '' then ''else' where '+@where end
    ,@where2=case isnull(@where,'')when '' then ''else ' and '+@where end
      ,@OrderCol=case isnull(@OrderCol,'') when '' then '' else ' order by '+@OrderCol endIF(@OrderType=1)
    SET @OrderCol=@OrderCol+' desc'--如果显示第一页,可以直接用top来完成
    if @PageCurrent=1 
    begin
     select @Id1=cast(@PageSize as varchar(20))
     exec('select top '+@Id1+@ShowList+' from '+@TableName+@where1+@OrderCol)
     return
    end exec('select top '+@Id1+@ShowList+' from '+@TableName
      +' where '+@PrimaryKey+' not in(select top '
      +@Id2+' '+@PrimaryKey+' from '+@TableName+@where1+@OrderCol
      +')'+@where2+@OrderCol
      )
     return
    GO
      

  2.   

    没发现问题,按照你的数据
    出来的结果就是100,300,500。
    你可以试试第一页的数据是按照这个语句执行的,
    select * from (SELECT TOP 3 * FROM UserInfo ORDER BY UserID) a order by point 
    你自己看错数据了。
      

  3.   

    http://community.csdn.net/Expert/topic/3587/3587201.xml?temp=8.866519E-02這裡自己看。
      

  4.   

    我是在应用中出现问题的,一开始我没怀疑是存储过程的问题,后来在查询分析器里调试了好多遍,才肯定是存储过程的问题.
    这个存储过程是在pbsql博客的回复上找到的.对他存储过程加了排序列的排序方法.不知是谁写的