What is the best way to paginate results in SQL Server

前端 未结 19 2495
我寻月下人不归
我寻月下人不归 2020-11-22 01:36

What is the best way (performance wise) to paginate results in SQL Server 2000, 2005, 2008, 2012 if you also want to get the total number of results (before paginating)?

19条回答
  •  悲哀的现实
    2020-11-22 01:53

    create PROCEDURE SP_Company_List (@pagesize int = -1 ,@pageindex int= 0   ) > AS BEGIN  SET NOCOUNT ON;
    
    
        select  Id , NameEn     from Company  ORDER by Id ASC  
    OFFSET (@pageindex-1 )* @pagesize   ROWS FETCH NEXt @pagesize ROWS ONLY END  GO
    

    DECLARE   @return_value int
    
    EXEC  @return_value = [dbo].[SP_Company_List]         @pagesize = 1 ,         > @pageindex = 2
    
    SELECT    'Return Value' = @return_value
    
    GO
    

提交回复
热议问题