SQL基础分页存储过程(案例一)

 1 --分页 存储过程 案例
 2 
 3      -- 所执行的存储过程
 4     create proc pageForUsers
 5     @currPage int,        --当前页数
 6     @pageSize int,        --每页多少条记录
 7     @count int output    --总记录数    
 8     as
 9     declare @firstIndex  int
10     declare @lastIndex  int
11     declare @sqlText varchar(200)
12 
13     --统计总记录数
14     select @count=count(*) from users
15     --首先计算当前页第一条记录索引
16     set @firstIndex=(@currPage - 1) * @pageSize + 1
17     --当前页最后一条记录索引
18     set @lastIndex = @firstIndex + @pageSize - 1
19     --显示结果
20     set @sqlText='select top ' + convert(varchar(5),@pageSize) + ' * from users
21         where [id] not in (select top ' + convert(varchar(5),@firstIndex-1) + '[id] from users)'
22     exec (@sqlText)
23     
原文地址:https://www.cnblogs.com/cykj/p/SQL-based-paging-stored-procedure-case1.html