|
CREATE PROCEDURE List
@PageIndex INT, --Enter the current page number
@PageSize INT, --Number of records per page
@RecordCount INT OUT, --Number of records
@PageCount INT OUT - number of pages
AS
SELECT @RecordCount = COUNT(*) FROM BLOG_Info INNER JOIN LOG_User ON BLOG_Info.uid = BLOG_User.uid
SET @PageCount = CEILING(@RecordCount * 1.0 / @PageSize)
DECLARE @SQLSTR NVARCHAR(1000)
IF @PageIndex = 0 OR @PageCount <= 1 - first page
set @SQLSTR = "select top "+STR( @PageSize )+" BLOG_Info.title, BLOG_Info.detail, BLOG_User.uname, BLOG_Info.dtime, BLOG_User.url, BLOG_Info.iid FROM BLOG_Info INNER JOIN BLOG_User ON BLOG_Info.uid = BLOG_User .uid order by BLOG_Info.dtime desc"
else if @PageIndex = @PageCount-1 - last page
set @SQLSTR = "select top "+STR( @PageSize )+"BLOG_Info.title, BLOG_Info.detail, BLOG_User.uname, BLOG_Info.dtime, BLOG_User.url, BLOG_Info.iid FROM BLOG_Info INNER JOIN BLOG_User ON BLOG_Info.uid = BLOG_User .uid where BLOG_Info.iid>(select max(BLOG_Info.iid) from (select top "+STR(@PageSize * @PageIndex )+" BLOG_Info.title, BLOG_Info.detail, BLOG_User.uname, BLOG_Info.dtime, BLOG_User.url , BLOG_Info.iid FROM BLOG_Info INNER JOIN BLOG_User ON BLOG_Info.uid = BLOG_User.uid order by BLOG_Info.dtime desc )) order by BLOG_Info.dtime desc"
else
set @SQLSTR = "select top "+STR( @PageSize )+"BLOG_Info.title, BLOG_Info.detail, BLOG_User.uname, BLOG_Info.dtime, BLOG_User.url, BLOG_Info.iid FROM BLOG_Info INNER JOIN BLOG_User ON BLOG_Info.uid = BLOG_User .uid where BLOG_Info.iid>(select max(BLOG_Info.iid) from (select top "+STR(@PageSize * @PageIndex )+" BLOG_Info.title, BLOG_Info.detail, BLOG_User.uname, BLOG_Info.dtime, BLOG_User.url , BLOG_Info.iid FROM BLOG_Info INNER JOIN BLOG_User ON BLOG_Info.uid = BLOG_User.uid order by BLOG_Info.dtime desc )) order by BLOG_Info.dtime desc"
EXEC (@SQLSTR)
GO
This is my stored procedure to see if it is wrong. |
|