请先阅读原文:http://blog.csdn.net/qiaqia609/article/details/41445233

代码 复制 - 运行

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[sys_Page_v2]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)  
drop procedure [dbo].[sys_Page_v2]  
GO  
CREATE PROCEDURE [dbo].[sys_Page_v2]  
@PCount int output,    --总页数输出  
@RCount int output,    --总记录数输出  
@sys_Table nvarchar(100),    --查询表名  
@sys_Key varchar(50),        --主键  
@sys_Fields nvarchar(500),    --查询字段  
@sys_Where nvarchar(3000),    --查询条件  
@sys_Order nvarchar(100),    --排序字段  
@sys_Begin int,        --开始位置  
@sys_PageIndex int,        --当前页数  
@sys_PageSize int        --页大小  
AS  
SET NOCOUNT ON  
SET ANSI_WARNINGS ON  
IF @sys_PageSize < 0 OR @sys_PageIndex < 0  
BEGIN          
RETURN  
END  
DECLARE @new_where1 NVARCHAR(3000)  
DECLARE @new_order1 NVARCHAR(100)  
DECLARE @new_order2 NVARCHAR(100)  
DECLARE @Sql NVARCHAR(4000)  
DECLARE @SqlCount NVARCHAR(4000)  
DECLARE @Top int  
if(@sys_Begin <=0)  
    set @sys_Begin=0  
else  
    set @sys_Begin=@sys_Begin-1  
IF ISNULL(@sys_Where,'') = ''  
    SET @new_where1 = ' '  
ELSE  
    SET @new_where1 = ' WHERE ' + @sys_Where  
IF ISNULL(@sys_Order,'') <> ''   
BEGIN  
    SET @new_order1 = ' ORDER BY ' + Replace(@sys_Order,'desc','')  
    SET @new_order1 = Replace(@new_order1,'asc','desc')  
    SET @new_order2 = ' ORDER BY ' + @sys_Order  
END  
ELSE  
BEGIN  
    SET @new_order1 = ' ORDER BY ID DESC'  
    SET @new_order2 = ' ORDER BY ID ASC'  
END  
SET @SqlCount = 'SELECT @RCount=COUNT(1),@PCount=CEILING((COUNT(1)+0.0)/'  
            + CAST(@sys_PageSize AS NVARCHAR)+') FROM ' + @sys_Table + @new_where1  
EXEC SP_EXECUTESQL @SqlCount,N'@RCount INT OUTPUT,@PCount INT OUTPUT',  
               @RCount OUTPUT,@PCount OUTPUT  
IF @sys_PageIndex > CEILING((@RCount+0.0)/@sys_PageSize)    --如果输入的当前页数大于实际总页数,则把实际总页数赋值给当前页数  
BEGIN  
    SET @sys_PageIndex =  CEILING((@RCount+0.0)/@sys_PageSize)  
END  
set @sql = 'select '+ @sys_fields +' from ' + @sys_Table + ' w1 '  
    + ' where '+ @sys_Key +' in ('  
        +'select top '+ ltrim(str(@sys_PageSize)) +' ' + @sys_Key + ' from '  
        +'('  
            +'select top ' + ltrim(STR(@sys_PageSize * @sys_PageIndex + @sys_Begin)) + ' ' + @sys_Key + ' FROM '  
        + @sys_Table + @new_where1 + @new_order2   
        +') w ' + @new_order1  
    +') ' + @new_order2  
print(@sql)  
Exec(@sql)  
GO  



问题:
1、最后一页有问题
2、sys_Begin 可以无需输入




=================小鱼修正==================

代码 复制 - 运行

/****** Object:  StoredProcedure [dbo].[sys_Page_v2]    Script Date: 10/10/2017 11:58:45 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


--http://blog.csdn.net/qiaqia609/article/details/41445233
CREATE PROCEDURE [dbo].[sys_Page_v2]  
@PCount int output,    --总页数输出  
@RCount int output,    --总记录数输出  
@sys_Table nvarchar(100),    --查询表名  
@sys_Key varchar(50),        --主键  
@sys_Fields nvarchar(500),    --查询字段  
@sys_Where nvarchar(3000),    --查询条件  
@sys_Order nvarchar(500),    --排序字段  
@sys_Begin int,        --开始位置  
@sys_PageIndex int,        --当前页数  
@sys_PageSize int        --页大小  
AS  
SET NOCOUNT ON  
SET ANSI_WARNINGS ON  
IF @sys_PageSize < 0 OR @sys_PageIndex < 0  
BEGIN          
RETURN  
END
--执行毫秒开始
declare @d1 DateTime
set @d1=GETDATE()

DECLARE @new_where1 NVARCHAR(3000)  
DECLARE @new_order1 NVARCHAR(100)  
DECLARE @new_order2 NVARCHAR(100)  
DECLARE @Sql NVARCHAR(4000)  
DECLARE @SqlCount NVARCHAR(4000)  
DECLARE @Top int 

/*
if(@sys_Begin <=0)  
    set @sys_Begin=0  
else  
    set @sys_Begin=@sys_Begin-1  
*/  
if(@sys_PageIndex <=0)  
    set @sys_PageIndex=1 
set @sys_Begin=@sys_PageIndex-1
--以上为小鱼修正,@sys_Begin可以不用输入,其实现在已不接受参数


IF ISNULL(@sys_Where,'') = ''  
    SET @new_where1 = ' '  
ELSE  
    SET @new_where1 = ' WHERE ' + @sys_Where  
IF ISNULL(@sys_Order,'') <> ''   
BEGIN  
    SET @new_order1 = ' ORDER BY ' + Replace(@sys_Order,'desc','')  
    SET @new_order1 = Replace(@new_order1,'asc','desc')  
    SET @new_order2 = ' ORDER BY ' + @sys_Order  
END  
ELSE  
BEGIN  
    SET @new_order1 = ' ORDER BY ID DESC'  
    SET @new_order2 = ' ORDER BY ID ASC'  
END  

SET @SqlCount = 'SELECT @RCount=COUNT(1),@PCount=CEILING((COUNT(1)+0.0)/'  
            + CAST(@sys_PageSize AS NVARCHAR)+') FROM ' + @sys_Table + @new_where1  
EXEC SP_EXECUTESQL @SqlCount,N'@RCount INT OUTPUT,@PCount INT OUTPUT',  
               @RCount OUTPUT,@PCount OUTPUT  

IF @sys_PageIndex > CEILING((@RCount+0.0)/@sys_PageSize)    --如果输入的当前页数大于实际总页数,则把实际总页数赋值给当前页数  
BEGIN  
    SET @sys_PageIndex =  CEILING((@RCount+0.0)/@sys_PageSize)  
END  
/*
set @sql = 'select '+ @sys_fields +' from ' + @sys_Table + ' w1 '  
    + ' where '+ @sys_Key +' in ('  
        +'select top '+ ltrim(str(@sys_PageSize)) +' ' + @sys_Key + ' from '  
        +'('  
            +'select top ' + ltrim(STR(@sys_PageSize * @sys_PageIndex + @sys_Begin)) + ' ' + @sys_Key + ' FROM '  
        + @sys_Table + @new_where1 + @new_order2   
        +') w ' + @new_order1  
    +') ' + @new_order2  
print(@sql)  
Exec(@sql)  
*/
DECLARE @ThisTopList int = @sys_PageSize --当前页需要列多少条
DECLARE @ThisList int = @sys_PageSize * @sys_PageIndex --当前页计算来的条数
--print(@ThisList)  
--print(@RCount) 
IF @ThisList > @RCount
 SET @ThisTopList = @sys_PageSize - (@ThisList - @RCount)

set @sql = 'select '+ @sys_fields +' from ' + @sys_Table + ' w1 '  
    + ' where '+ @sys_Key +' in ('  
        +'select top '+ ltrim(str(@ThisTopList)) +' ' + @sys_Key + ' from '  
        +'('  
            +'select top ' + ltrim(STR(@ThisList + @sys_Begin)) + ' ' + @sys_Key + ' FROM '  
        + @sys_Table + @new_where1 + @new_order2   
        +') w ' + @new_order1  
    +') ' + @new_order2  
--print(@sql) 
Exec(@sql)

--执行毫秒计算输出
declare @d21 DateTime
set @d21=GETDATE()
print(datediff(ms,@d1,@d21)) 


GO