T-SQL查询语句批量返回记录

时间:2012-06-15 00:16:25

标签: sql-server-2008 tsql

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

1 个答案:

答案 0 :(得分:0)

请尝试测试以下内容

只需传递pageindex和pagesize 什么时候

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))