给浩方的评论系统写的.
实现了四个分页状态
1 请求页 = 第一页
2 请求页 <= 总页数/2
3 请求页 > 总页数/2
4 请求页 = 最后页
不过感觉 最后页的实现不是很好 不知道有什么简单的办法没.
还有 但请求页 = 总页数/2 的时候又不知道用什么来比较简单.
本来天极上面有一篇分析的文章上次看了 忘了 再页找不到了.
看来有必要自己写个小系统来管理自己的东西了.
/*
作者: SLIGHTBOY
建立日期: 2004-12-3
修改日期: 2004-12-8
修改日期: 2004-12-9
修改日期: 2004-12-10
修改日期: 2004-12-13
存储作用: 新闻评论列表
作用数据库: [Cga_News_Comment_t]
输入变量:
@Sort_ID 引用编号
@Comment_Approve 显示类型
@PageSize 一页包含的记录数目
@PageRequest 请求页数
@PageCount 数据页数
@AbsolutePage 当前页
@RecordCount 记录统计
输出变量:
*/
CREATE PROCEDURE dbo.sp_CGA_NewsComment_List
@Sort_ID INT,
@Comment_Approve BIT = NULL,
@PageSize INT = 20,
@PageRequest INT = 1,
@PageCount INT = 0 OUTPUT,
@AbsolutePage INT = 1 OUTPUT,
@RecordCount INT = 0 OUTPUT
AS
DECLARE @SqlQueryString VARCHAR(8000)
DECLARE @SqlQueryCondition VARCHAR(200)
-- 初始化一般信息
SET @AbsolutePage = @PageRequest
-- 查询设置
-- DECLARE @SqlQueryKey VARCHAR(20)
-- DECLARE @SqlQueryColumn VARCHAR(200)
-- DECLARE @SqlQueryTable VARCHAR(20)
-- Start 查询初始化
SET NOCOUNT ON
-- 得到初始信息
IF (@Comment_Approve <> NULL)
BEGIN
-- 查询条件
SET @SqlQueryCondition = 'Sort_ID = '+ STR(@Sort_ID) +' AND Comment_Approve = '+ STR(@Comment_Approve)
-- 计算评论总数
SELECT @RecordCount = COUNT(Comment_Id)
FROM [Cga_News_Comment_t]
WHERE Sort_ID = @Sort_ID AND Comment_Approve = @Comment_Approve
END
Else
BEGIN
-- 查询条件
SET @SqlQueryCondition = 'Sort_ID = '+ STR(@Sort_ID)
-- 计算评论总数
SELECT @RecordCount = COUNT(Comment_Id)
FROM [Cga_News_Comment_t]
WHERE Sort_ID = @Sort_ID
END
-- 得到评论总页数
SET @PageCount = CEILING( CAST(@RecordCount AS FLOAT)/@PageSize )
-- End 查询初始化
-- Start 分页逻辑
IF ( @PageRequest = 1 )
BEGIN
SET @AbsolutePage = 1
SET @SqlQueryString =
'SELECT TOP '+ STR(@PageSize) +' Comment_Id, User_Id, User_Name, Comment_Content, User_Ip, Add_Date,Comment_Approve
FROM [Cga_News_Comment_t]
WHERE '+ @SqlQueryCondition +'
ORDER BY Comment_Id DESC'
END
Else IF ( @PageRequest >= @PageCount )
BEGIN
SET @AbsolutePage = @PageCount
SET @SqlQueryString =
'SELECT TOP '+ STR(@PageSize) +' Comment_Id, User_Id, User_Name, Comment_Content, User_Ip, Add_Date,Comment_Approve
FROM [Cga_News_Comment_t]
WHERE '+ @SqlQueryCondition +' AND Comment_Id <=
( SELECT MAX(Comment_Id)
FROM
( SELECT TOP'+ STR(@RecordCount - ABS(@PageSize * (@PageCount - 1)) ) +' Comment_Id
FROM [Cga_News_Comment_t]
WHERE '+ @SqlQueryCondition +'
ORDER BY Comment_Id ASC
) AS TableTemp
)
ORDER BY Comment_Id DESC'
END
Else IF ( @PageRequest <= @PageCount / 2)
BEGIN
SET @SqlQueryString =
'SELECT TOP '+ STR(@PageSize) +' Comment_Id, User_Id, User_Name, Comment_Content, User_Ip, Add_Date,Comment_Approve
FROM [Cga_News_Comment_t]
WHERE '+ @SqlQueryCondition +' AND Comment_Id <
( SELECT MIN(Comment_Id)
FROM
( SELECT TOP'+ STR(@PageSize * (@PageRequest - 1)) +' Comment_Id
FROM [Cga_News_Comment_t]
WHERE '+ @SqlQueryCondition +'
ORDER BY Comment_Id DESC
) AS TableTemp
)
ORDER BY Comment_Id DESC'
END
Else IF ( @PageRequest > @PageCount / 2)
BEGIN
SET @SqlQueryString =
'SELECT TOP '+ STR(@PageSize) +' Comment_Id, User_Id, User_Name, Comment_Content, User_Ip, Add_Date,Comment_Approve
FROM [Cga_News_Comment_t]
WHERE '+ @SqlQueryCondition +' AND Comment_Id <
( SELECT MAX(Comment_Id)
FROM
( SELECT TOP'+ STR(@RecordCount - (@PageSize * (@PageRequest - 1) - 1)) +' Comment_Id
FROM [Cga_News_Comment_t]
WHERE '+ @SqlQueryCondition +'
ORDER BY Comment_Id ASC
) AS TableTemp
)
ORDER BY Comment_Id DESC'
END
Else
BEGIN
-- 目前没有实现此逻辑
PRINT @SqlQueryString
END
-- PRINT @SqlQueryString
-- SELECT @RecordCount AS '@RecordCount', @PageSize AS '@PageSize', @PageRequest AS '@PageRequest'
EXEC (@SqlQueryString)
GO