         (@CategoryID is null or cat.CategoryID=@CategoryID) and
         c.IsDeleted = 0
         and	c.TopicMovedID is null	
-    order by
-        c.LastPosted desc ,
-        cat.SortOrder asc,
-        d.SortOrder asc,
-        d.Name asc,
-        c.Priority desc		
-    
-    set rowcount @PageSize	
-            select
-        c.ForumID,
+	  )	  
+	  select
+		c.ForumID,
         c.TopicID,
         c.TopicMovedID,		
         c.Posted,

             when 1 then
               (SELECT top 1 LastAccessDate FROM [{databaseOwner}].[{objectQualifier}TopicReadTracking] y WHERE y.TopicID=c.TopicID AND y.UserID = @PageUserID)
             else ''	 end,
        TotalRows = @post_totalrowsnumber,
        PageIndex = @PageIndex
    from
        [{databaseOwner}].[{objectQualifier}Topic] c
        join [{databaseOwner}].[{objectQualifier}User] b on b.UserID=c.UserID
        join [{databaseOwner}].[{objectQualifier}Forum] d on d.ForumID=c.ForumID
        join [{databaseOwner}].[{objectQualifier}ActiveAccess] x  with(nolock) on x.ForumID=d.ForumID
        join [{databaseOwner}].[{objectQualifier}Category] cat on cat.CategoryID=d.CategoryID
    where
        c.LastPosted <= @firstselectposted and
        x.UserID = @PageUserID and
        CONVERT(int,x.ReadAccess) <> 0 and
        cat.BoardID = @BoardID and
        (@CategoryID is null or cat.CategoryID=@CategoryID) and
        c.IsDeleted = 0
        and	c.TopicMovedID is null
    order by
        c.LastPosted desc,
        cat.SortOrder asc,
        d.SortOrder asc,
        d.Name asc,
        c.Priority desc	

        SET ROWCOUNT 0		
    
end
GO

)
AS
begin
declare @TotalRows int
   declare @FirstSelectRowNumber int
   declare @LastSelectRowNumber int
   
   -- find total returned count
   select  @TotalRows = count(1)		
        from
        [{databaseOwner}].[{objectQualifier}Topic] c
        join [{databaseOwner}].[{objectQualifier}User] b on b.UserID=c.UserID
