user1030181
user1030181

Reputation: 2015

Sorting in Result Set with Paging SQL SERVER 2008

                  SELECT F.Id,   
                       F.FolderNo,
                       DENSE_RANK() OVER (ORDER BY  F.Id) AS RN  
                INTO #Results4                                  
                FROM   cm.pfmfolder F WITH(nolock)   


  SELECT DISTINCT * FROM #Results4  
   WHERE RN BETWEEN(@PageIndex -1) * @PageSize + 1 AND(((@PageIndex -1) * @PageSize + 1) + @PageSize) - 1  

   DROP TABLE #Results4 

Here the records are displaying as per the startIndex specified and PageSize, But when it's went to next page records are not sorted with FolderNo, is there any way that i can sort and do paging for this query

Upvotes: 1

Views: 51

Answers (1)

zhongxiao37
zhongxiao37

Reputation: 987

Simply add

order by FolderNo

Here is the example: SQL Fiddle Demo

Upvotes: 1

Related Questions