分页存储过程练习(一)

 1/*
 2说明:
 3(1)这里以northwind数据库中products表为例子(SQL2000)
 4(2)关于排序的功能没有完成
 5(3)数据库动态改变没有完成
 6*/

 7
 8--分页存储过程
 9alter procedure SplitPage
10@PageSize int,--每页大小
11@PageIndex int --当前页
12as
13declare @totalrecord int --总的记录数
14declare @totalpage int --总的页数
15select @totalrecord=count(*) from products
16set @totalpage=ceiling(cast(@totalrecord as float)/@PageSize)
17if @PageIndex<=@totalpage
18begin
19 declare @Sql varchar(1000)
20 if(@PageIndex=1)
21 begin
22  set @Sql='select top '+cast(@PageSize as varchar(100))+' * from products
23   order by productid asc'
24  execute(@Sql) 
25 end
26 else
27 begin 
28  set @PageIndex=(@PageIndex-1)*@PageSize
29  print @PageIndex
30  set @Sql='select top '+cast(@PageSize as varchar(100))+' * from products
31   where productid>all(select top '+cast(@PageIndex as varchar(100))+
32   ' productid from products order by productid asc) order by productid
33   asc'
34  execute(@Sql)
35 end
36
37end
38else
39begin
40
41 print '超出范围'
42end
43
44
联系我,QQ:271059875
原文地址:https://www.cnblogs.com/wang123/p/490556.html