现在的位置: 首页 > 综合 > 正文

在Sql Server中数据分页

2013年05月07日 ⁄ 综合 ⁄ 共 583字 ⁄ 字号 评论关闭

方法一:

    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) 

【上篇】
【下篇】

抱歉!评论已关闭.