-------------------------- -- 分页代码IF EXISTS (SELECT name FROM sysobjects WHERE name = N'p_GetTopic' AND type = 'P') DROP PROCEDURE p_GetTopic GO CREATE PROCEDURE p_GetTopic @PageSize int =15, @CurPage int =1, @OrderBy varchar(50) = 'desc' AS declare @sql varchar(4000) --declare @intCount int --select @intCount = count(*) from t_Issue if LOWER(@OrderBy) = 'asc' begin select @sql= 'select ti.*, tu.username, tu.usernickname, tu.ranknum, tu.credit, tr.roomname from (select top ' + cast(@PageSize as varchar(20)) + ' * from t_issue where topicid > isnull((select max(topicid) from (select top ' + cast((@PageSize * (@CurPage -1) ) as varchar(20)) + ' * from t_Issue order by topicid ) a),0) order by topicid) ti left join t_user tu on ti.postuserid = tu.userid left join t_room tr on ti.roomid = tr.roomid order by ti.topicid ' end else begin select @sql= 'select ti.*, tu.username, tu.usernickname, tu.ranknum, tu.credit, tr.roomname from (select top ' + cast(@PageSize as varchar(20)) + ' * from t_issue where topicid < isnull((select min(topicid) from (select top ' + cast((@PageSize * (@CurPage -1) ) as varchar(20)) + ' * from t_Issue order by topicid desc) a),99999999) order by topicid desc) ti left join t_user tu on ti.postuserid = tu.userid left join t_room tr on ti.roomid = tr.roomid order by ti.topicid desc ' end execute(@sql) select count(*) as RecordCount from t_issue GO -- ============================================= -- example to execute the store procedure -- ============================================= EXECUTE p_GetTopic 233,1 GO
|