在Sql Server中数据分页

方法一:

    DECLARE @rowsPerPage int, 
    @pageNum int, 
    @startRow int, 
    @endRow int
    SET @rowsPerPage = 10 
    SET @pageNum = 3 
    SET @startRow = ((@pageNum- 1) * @rowsPerPage)+1 
    SET @endRow = @startRow + @rowsPerPage -1 
    SELECT * FROM ( 
    SELECT row_number() OVER (ORDER BY id) as resultNum, 
    id FROM myTable 
    ) as numberResults 
    WHERE resultNum BETWEEN @startRow AND @endRow

方法二(推荐):

    SET @rowsPerPage = 10 
    SET @pageNum = 3 
    With SQLPaging 
    As
    ( 
    Select Top(@rowsPerPage * @pageNum) ROW_NUMBER() OVER (ORDER BY id) as resultNum, id 
    FROM myTable 
    ) 
    select * from SQLPaging where resultNum > ((@pageNum - 1) * @rowsPerPage) 

posted @ 2011-07-25 08:30  古月山谷  阅读(242)  评论(0编辑  收藏  举报