查询第X页,每页Y条记录最基本的处理方法(原理):如果表中有主键(记录不重复的字段也可以),可以用类似下面的方法,当然y,(x-1)*y要换成具体的数字,不能用变量:select top y * from 表 where 主键 not in(select top (x-1)*y 主键 from 表)如果表中无主键,可以用临时表,加标识字段解决.这里的x,y可以用变量.select id=identity(int,1,1),*  into #tb from 表
select * from #tb where id between (x-1)*y and x*y-1

解决方案 »

  1.   

    if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[sp_PageView]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
    drop procedure [dbo].[sp_PageView]
    GO/*--利用SQL未公开的存储过程实现分页

    方法简单且效率高,已知的问题就是要多返回一个空的记录集 解决的方法是在前台调用时,用 set recordset=recordset.nextrecordset
    的方法跳过第一个记录集

    此方法由J9988提供,我只是将它改成了方便调用的存储过程--邹建 2004.05(引用请保留此信息)--*//*--调用示例

    declare @PageCount int
    exec sp_PageView 
    @sql='select * from sysobjects',
    @PageCurrent=2,
    @PageCount=@PageCount out
    SELECT @PageCount
    --*/
    CREATE PROC sp_PageView   
    @sql         ntext,     --要执行的sql语句
    @PageCurrent int=1,     --要显示的页码
    @PageSize    int=10,    --每页的大小
    @PageCount   int OUTPUT --总页数
    AS
    SET NOCOUNT ON
    DECLARE @p1 int
    --初始化分页游标
    EXEC sp_cursoropen 
    @cursor=@p1 OUTPUT,
    @stmt=@sql,
    @scrollopt=1,
    @ccopt=1,
    @rowcount=@PageCount OUTPUT--计算总页数
    IF ISNULL(@PageSize,0)<1 
    SET @PageSize=10
    SET @PageCount=(@PageCount+@PageSize-1)/@PageSize
    IF ISNULL(@PageCurrent,0)<1 OR ISNULL(@PageCurrent,0)>@PageCount
    SET @PageCurrent=1
    ELSE
    SET @PageCurrent=(@PageCurrent-1)*@PageSize+1--显示指定页的数据
    EXEC sp_cursorfetch @p1,16,@PageCurrent,@PageSize--关闭分页游标
    EXEC sp_cursorclose @p1
    GO
      

  2.   

    2005可以直接用rownumber函数分页
      

  3.   

    引用他人的
    -- 获取指定页的数据 
    CREATE PROCEDURE pagination
    @tblName varchar(255), -- 表名 
    @strGetFields varchar(1000) = '*', -- 需要返回的列 
    @fldName varchar(255)='', -- 排序的字段名 
    @PageSize int = 10, -- 页尺寸 
    @PageIndex int = 1, -- 页码 
    @doCount bit = 0, -- 返回记录总数, 非 0 值则返回 
    @OrderType bit = 0, -- 设置排序类型, 非 0 值则降序 
    @strWhere varchar(1500) = '' -- 查询条件 (注意: 不要加 where) 
    AS declare @strSQL varchar(5000) -- 主语句 
    declare @strTmp varchar(110) -- 临时变量 
    declare @strOrder varchar(400) -- 排序类型 
    if @doCount != 0 
    begin 
    if @strWhere !='' 
    set @strSQL = "select count(*) as Total from [" + @tblName + "] where "+@strWhere 
    else 
    set @strSQL = "select count(*) as Total from [" + @tblName + "]" 
    end 
    --以上代码的意思是如果@doCount传递过来的不是0,就执行总数统计。以下的所有代码都是@doCount为0的情况 else 
    begin 
    if @OrderType != 0 
    begin 
    set @strTmp = "<(select min" 
    set @strOrder = " order by [" + @fldName +"] desc" 
    --如果@OrderType不是0,就执行降序,这句很重要! 
    end else 
    begin 
    set @strTmp = ">(select max" 
    set @strOrder = " order by [" + @fldName +"] asc" 
    end 
    if @PageIndex = 1 
    begin 
    if @strWhere != '' 
    set @strSQL = "select top " + str(@PageSize) +" "+@strGetFields+ " from [" + @tblName + "] where " + @strWhere + " " + @strOrder 
    else 
    set @strSQL = "select top " + str(@PageSize) +" "+@strGetFields+ " from ["+ @tblName + "] "+ @strOrder 
    --如果是第一页就执行以上代码,这样会加快执行速度 
    end else 
    begin 
    --以下代码赋予了@strSQL以真正执行的SQL代码 
    set @strSQL = "select top " + str(@PageSize) +" "+@strGetFields+ " from [" 
    + @tblName + "] where [" + @fldName + "]" + @strTmp + "(["+ @fldName + "]) from (select top " + str((@PageIndex-1)*@PageSize) + " ["+ @fldName + "] from [" + @tblName + "]" + @strOrder + ") as tblTmp)"+ @strOrder 
    if @strWhere != '' 
    set @strSQL = "select top " + str(@PageSize) +" "+@strGetFields+ " from [" 
    + @tblName + "] where [" + @fldName + "]" + @strTmp + "([" 
    + @fldName + "]) from (select top " + str((@PageIndex-1)*@PageSize) + " [" 
    + @fldName + "] from [" + @tblName + "] where " + @strWhere + " " 
    + @strOrder + ") as tblTmp) and " + @strWhere + " " + @strOrder 
    end 
    end 
    exec (@strSQL)
    GO
      

  4.   

    请问大哥,在jsp里面应该怎么调用
    /*--调用示例declare @PageCount int
    exec sp_PageView 
    @sql='select * from sysobjects',
    @PageCurrent=2,
    @PageCount=@PageCount out
    SELECT @PageCount
    --*/