/*
 名称:spAll_ReturnRows
 输入:
 输出:
 调用:
  EXEC spAll_ReturnRows 'SELECT  * FROM 表名', 页号, 返回记录数, '主键', '排序字段'
  spAll_ReturnRows 'SELECT  * FROM all_Categories',2,10,'[ID]','[ID]'
 说明:[百万级]通用存储过程.分页存储过程..返回指定返回条数、指定页数的记录
 作者:Dili J.F. Senders
  邮件:diliatwellknow.net
  网站:http://www.wellknow.net
  更新:20040610
  支持:http://bbs.wellknow.net
  版权:转述时请注明来源:用思维创造未来的Wellknow.net
*/

CREATE PROCEDURE dbo.spAll_ReturnRows
(
 @SQL nVARCHAR(4000),
 @Page int,
 @RecsPerPage int,
 @ID VARCHAR(255),
 @Sort VARCHAR(255)
)
AS

DECLARE @Str nVARCHAR(4000)

SET @Str='SELECT   TOP '+CAST(@RecsPerPage AS VARCHAR(20))+' * FROM ('+@SQL+') T WHERE T.'+@ID+' NOT IN
(SELECT   TOP '+CAST((@RecsPerPage*(@Page-1)) AS VARCHAR(20))+' '+@ID+' FROM ('+@SQL+') T9 ORDER BY '+@Sort+') ORDER BY '+@Sort

PRINT @Str

EXEC sp_ExecuteSql @Str
GO