数据库分页存储过程(5)
/*
******************************************************************************************
过程名称:Common_Percent_Pagination
过程功能:
代码设计:小朱(zsy619@163.com)
设计时间:2005-11-3 13:58:26
******************************************************************************************
功能描述:
******************************************************************************************
如果您修改了我的程序,请留下修改记录,以便对程序进行维护,谢谢 !!!
==========================================================================================
修改人 修改时间 修改原因
------------------------------------------------------------------------------------------
==========================================================================================
******************************************************************************************
备注:
*****************************************************************************************
*/
Create Procedure [dbo].[Common_Percent_Pagination]
@TBName NVARCHAR(255) ,
@SQL nVARCHAR(4000), --不带排序语句的SQL语句
@Page int, --页码
@RecsPerPage int, --每页容纳的记录数
@ID VARCHAR(255), --需要排序的不重复的ID号
@Sort VARCHAR(255) , --排序字段及规则
@PageCount INT OUTPUT --总页数
AS
Declare @intResult Int
Begin Tran
-----------------------------------------------------------------代码设计--------------------------------------------------------------------
DECLARE @sql1 nvarchar(4000)
SET @sql1=N'SELECT @PageCount=COUNT(*)'
+N' FROM '+@tbname
EXEC sp_executesql @sql1,N'@PageCount int OUTPUT',@PageCount OUTPUT
SET @PageCount=(@PageCount+@RecsPerPage-1)/@RecsPerPage
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
--EXEC @Str
DECLARE @Str1 NVARCHAR(400)
DECLARE @Str2 NVARCHAR(400)
SET @Str1 = CAST(@RecsPerPage AS VARCHAR(20))
SET @Str2 = CAST((@RecsPerPage*(@Page-1)) AS VARCHAR(20))
EXEC ( N'SELECT TOP '+@Str1+ N' * FROM ('+@SQL+N') T WHERE T.'+@ID+N' NOT IN (SELECT TOP '+@Str2+N' '+@ID+N' FROM ('+@SQL+N') T9 ORDER BY '+@Sort+N') ORDER BY '+@Sort )
Set @intResult = @@ROWCOUNT
----------------------------------------------------------------------------------------------------------------------------------------------------
If @@Error <> 0
Begin
RollBack Tran
Return -1
End
Else
Begin
Commit Tran
Return @intResult
End
GO
******************************************************************************************
过程名称:Common_Percent_Pagination
过程功能:
代码设计:小朱(zsy619@163.com)
设计时间:2005-11-3 13:58:26
******************************************************************************************
功能描述:
******************************************************************************************
如果您修改了我的程序,请留下修改记录,以便对程序进行维护,谢谢 !!!
==========================================================================================
修改人 修改时间 修改原因
------------------------------------------------------------------------------------------
==========================================================================================
******************************************************************************************
备注:
*****************************************************************************************
*/
Create Procedure [dbo].[Common_Percent_Pagination]
@TBName NVARCHAR(255) ,
@SQL nVARCHAR(4000), --不带排序语句的SQL语句
@Page int, --页码
@RecsPerPage int, --每页容纳的记录数
@ID VARCHAR(255), --需要排序的不重复的ID号
@Sort VARCHAR(255) , --排序字段及规则
@PageCount INT OUTPUT --总页数
AS
Declare @intResult Int
Begin Tran
-----------------------------------------------------------------代码设计--------------------------------------------------------------------
DECLARE @sql1 nvarchar(4000)
SET @sql1=N'SELECT @PageCount=COUNT(*)'
+N' FROM '+@tbname
EXEC sp_executesql @sql1,N'@PageCount int OUTPUT',@PageCount OUTPUT
SET @PageCount=(@PageCount+@RecsPerPage-1)/@RecsPerPage
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
--EXEC @Str
DECLARE @Str1 NVARCHAR(400)
DECLARE @Str2 NVARCHAR(400)
SET @Str1 = CAST(@RecsPerPage AS VARCHAR(20))
SET @Str2 = CAST((@RecsPerPage*(@Page-1)) AS VARCHAR(20))
EXEC ( N'SELECT TOP '+@Str1+ N' * FROM ('+@SQL+N') T WHERE T.'+@ID+N' NOT IN (SELECT TOP '+@Str2+N' '+@ID+N' FROM ('+@SQL+N') T9 ORDER BY '+@Sort+N') ORDER BY '+@Sort )
Set @intResult = @@ROWCOUNT
----------------------------------------------------------------------------------------------------------------------------------------------------
If @@Error <> 0
Begin
RollBack Tran
Return -1
End
Else
Begin
Commit Tran
Return @intResult
End
GO