if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[p_splitpage]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[p_splitpage]
GO--利用SQL未公开的存储过程实现分页
create procedure p_splitpage   
@sql nvarchar(4000), --要执行的sql语句
@currentpage int=2,  --要显示的页码
@pagesize int=10, --每页的大小
@pagecount int=0 out --总页数
as
set nocount on
declare @p1 intexec sp_cursoropen @p1 output,@sql,@scrollopt=1,@ccopt=1,@rowcount=@pagecount outputselect @pagecount=ceiling(1.0*@pagecount/@pagesize)
,@currentpage=(@currentpage-1)*@pagesize+1
select @currentpage
exec sp_cursorfetch @p1,16,@currentpage,@pagesize 
exec sp_cursorclose @p1
go--调用示例
exec p_splitpage 'select top 1000 id,name from sysobjects where 条件',2

解决方案 »

  1.   


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

  2.   

    查询第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
      

  3.   

    >>dll里面的函数使用string类型的变量应该问题不大吧?可以。我都是在内部使用string,只不过输入输出的时候才转为 pchar。