在SQL Server数据库操作中,我们常常会用到存储过程对实现对查询的数据的分页处理,以方便浏览者的浏览。

创建数据库data_Test :

create database data_Test  GO  use data_Test  GO  create table tb_TestTable  --创建表  (  id int identity(1,1) primary key,  userName nvarchar(20) not null,  userPWD nvarchar(20) not null,  userEmail nvarchar(40) null  )  GO 
set identity_insert tb_TestTable on  declare @count int  set@count=1  while @count<=2000000  begin  insert into tb_TestTable(id,userName,userPWD,userEmail) values(@count,'admin','admin888','lli0077@yahoo.com.cn')  set @count=@count+1  end  set identity_insert tb_TestTable off 

具体代码如下:

create procedure proc_paged_with_notin --利用select top and select not in  (  @pageIndex int, --页索引  @pageSize int  --每页记录数  )  as  begin  set nocount on;  declare @timediff datetime --耗时  declare @sql nvarchar(500)  select @timediff=Getdate()  set @sql='select top '+str(@pageSize)+' * from tb_TestTable where(ID not in(select top '+str(@pageSize*@pageIndex)+' id from tb_TestTable order by ID ASC)) order by ID'  execute(@sql) --因select top后不支技直接接参数,所以写成了字符串@sql  select datediff(ms,@timediff,GetDate()) as 耗时  set nocount off;  end 
create procedure proc_paged_with_selectMax --利用select top and select max(列)  (  @pageIndex int, --页索引  @pageSize int  --页记录数  )  as  begin  set nocount on;  declare @timediff datetime  declare @sql nvarchar(500)  select @timediff=Getdate()  set @sql='select top '+str(@pageSize)+' * From tb_TestTable where(ID>(select max(id) From (select top '+str(@pageSize*@pageIndex)+' id From tb_TestTable order by ID) as TempTable)) order by ID'  execute(@sql)  select datediff(ms,@timediff,GetDate()) as 耗时  set nocount off;  end 
create procedure proc_paged_with_Midvar --利用ID>最大ID值和中间变量  (  @pageIndex int,  @pageSize int  )  as  declare @count int  declare @ID int  declare @timediff datetime  declare @sql nvarchar(500)  begin  set nocount on;  select @count=0,@ID=0,@timediff=getdate()  select @count=@count+1,@ID=case when @count<=@pageSize*@pageIndex then ID else @ID end from tb_testTable order by id  set @sql='select top '+str(@pageSize)+' * from tb_testTable where ID>'+str(@ID)  execute(@sql)  select datediff(ms,@timediff,getdate()) as 耗时  set nocount off;  end 
create procedure proc_paged_with_Rownumber --利用SQL 2005中的Row_number()  (  @pageIndex int,  @pageSize int  )  as  declare @timediff datetime  begin  set nocount on;  select @timediff=getdate()  select * from (select *,Row_number() over(order by ID asc) as IDRank from tb_testTable) as IDWithRowNumber where IDRank>@pageSize*@pageIndex and IDRank<@pageSize*(@pageIndex+1)  select datediff(ms,@timediff,getdate()) as 耗时  set nocount off;  end
create procedure proc_CTE --利用临时表及Row_number  (  @pageIndex int, --页索引  @pageSize int  --页记录数  )  as  set nocount on;  declare @ctestr nvarchar()  declare @strSql nvarchar()  declare @datediff datetime  begin  select @datediff=GetDate()  set @ctestr='with Table_CTE as  (select ceiling((Row_number() over(order by ID ASC))/'+str(@pageSize)+') as page_num,* from tb_TestTable)';  set @strSql=@ctestr+' select * From Table_CTE where page_num='+str(@pageIndex)  end  begin  execute sp_executesql @strSql  select datediff(ms,@datediff,GetDate())  set nocount off;  end

更多相关文章

  1. 浅谈Java中Collections.sort对List排序的两种方法
  2. android分页查询获取系统联系人信息
  3. 浅谈RelativeLayout相对布局
  4. 浅谈android的selector背景选择器
  5. 浅谈android的selector背景选择器
  6. 浅谈android的selector背景选择器
  7. Android入门教程(三十一)------SQLite分页读取
  8. Android蓝牙开发浅谈
  9. android ListView的分段显示、分页显示(附源码)

随机推荐

  1. laravel 4路由::控制器()方法返回NotFoun
  2. 使用.php文件生成一个MySQL转储文件。
  3. 使用mod_rewrite将文件夹转换为查询字符
  4. PHP的钩子实现解析
  5. 准备好的报表没有速度效益吗?
  6. 上传文件时通过AJAX更新列表
  7. 现代 PHP 新特性 —— 闭包
  8. Mysql PHP生成的表:不能使用表
  9. PHP如何检查MySQLi查询是否需要关闭?
  10. 几个有用的php字符串过滤,转换函数代码