0

存储过程返回 1000000 条记录。如何通过传递参数来修改它并返回 1-100,101-200,201-300(即引入分页)。

4

1 回答 1

0

请尝试并测试以下内容

只需在传递 pageindex 和 pagessize 时

PageIndex = 1 and PageSize=100 => 1-100
PageIndex = 2 and PageSize=100 => 101-200 and so on


CREATE PROCEDURE [dbo].[MyProc]
(
@PageSize int,
@PageIndex int
)
AS
/* SET NOCOUNT ON */
declare 
    @error int,
    @StartRow int,
    @EndRow int

if(@PageIndex < 1)
    Set @PageIndex = 1

Set @StartRow = (@PageSize * (@PageIndex - 1)) + 1
Set @EndRow = @PageSize * @PageIndex

    SELECT * FROM(  
        SELECT
            col1,
            col2,
            col3
        FROM 
    SomeTable s
) s
WHERE ((s.RowNumber Between @StartRow AND @EndRow))
于 2012-06-15T00:44:14.077 回答