ALTER  procedure  [dbo].[selTestTable]  
@PageIndex                                                        int,  
@PageSize                                                        int  
 
as  
Declare  @rowcount                            numeric  
Declare  @intStart                            numeric  
set  nocount  on  
set  @intStart=(@PageIndex-1)*@PageSize+1  
if  @intStart=1  
           SELECT  @rowcount=count(ID)  from  TestTable  where  FirstName  like  '%aa%'  
else  
           set  @rowcount=0  
;  
 
WITH  PartsCTE  AS(Select  ROW_NUMBER()  OVER(order  by  ID)  as  row,  
           *  
                       From  TestTable  where  FirstName  like  '%aa%'  
)  
Select  *  
           From  PartsCTE  A  
             
           where  row  between  @intStart  and  @intStart+@PageSize-1  
return  @rowcount  
set  nocount  off