使用存储过程创建分页
2012-08-19 11:04
597 查看
在实际的开发过程中,经常遇到存储过程分页,下面根据实际情况总结的几种方法:
数据库名称:myTest
1、思路:利用select top and select not in 排除例外情况的分页
use myTest
go
create procedure proc_paged_with_notin
(
@pageIndex int, --页索引
@pageSize int --每页记录数
)
as
begin
set nocount on; --没有返回值
declare @sql nvarchar(500)
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
set nocount off;
end
exec proc_paged_with_notin 10,5
2、思路:利用select top and select max(列)分页
use myTest
go
create procedure proc_paged_with_selectMax
(
@pageIndex int, --页索引
@pageSize int --每页记录数
)
as
begin
set nocount on;
declare @sql nvarchar(500)
if @pageIndex=0
set @sql='select top '+str(@pageSize)+' * from tb_TestTable order by ID'
else
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)
set nocount off;
end
exec proc_paged_with_selectMax 10,5
3、思路:利用Row_number()给数据行加上索引
create procedure proc_paged_with_Rownumber --利用SQL 2005中的Row_number()
(
@pageIndex int,
@pageSize int
)
as
begin
set nocount on;
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)
set nocount off;
end
exec proc_paged_with_Rownumber 10,5
三种方法的测试结果显示:select max >row_number>not in(分页速度)
数据库名称:myTest
1、思路:利用select top and select not in 排除例外情况的分页
use myTest
go
create procedure proc_paged_with_notin
(
@pageIndex int, --页索引
@pageSize int --每页记录数
)
as
begin
set nocount on; --没有返回值
declare @sql nvarchar(500)
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
set nocount off;
end
exec proc_paged_with_notin 10,5
2、思路:利用select top and select max(列)分页
use myTest
go
create procedure proc_paged_with_selectMax
(
@pageIndex int, --页索引
@pageSize int --每页记录数
)
as
begin
set nocount on;
declare @sql nvarchar(500)
if @pageIndex=0
set @sql='select top '+str(@pageSize)+' * from tb_TestTable order by ID'
else
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)
set nocount off;
end
exec proc_paged_with_selectMax 10,5
3、思路:利用Row_number()给数据行加上索引
create procedure proc_paged_with_Rownumber --利用SQL 2005中的Row_number()
(
@pageIndex int,
@pageSize int
)
as
begin
set nocount on;
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)
set nocount off;
end
exec proc_paged_with_Rownumber 10,5
三种方法的测试结果显示:select max >row_number>not in(分页速度)
相关文章推荐
- 关于使用存储过程创建分页
- Oracle通用分页存储过程的创建与使用
- 关于使用存储过程创建分页
- 关于使用存储过程创建分页
- Oracle通用分页存储过程的创建与使用
- 关于使用存储过程创建分页
- Oracle通用分页存储过程的创建与使用
- 关于使用存储过程创建分页
- mssql:t-sql;创建表;给表添加约束;使用变量;事务,索引,视图;存储过程
- 使用存储过程查询并按每页10条记录分页显示图书借阅纪录
- 一、 使用存储过程实现数据分页(Sql Server 2008 R2)
- 使用存储过程创建datawindow
- ASP之简化创建关闭记录集对象并创建使用简单的MSSQL存储过程
- Python使用MySQLdb创建、调用MySQL存储过程
- sqlserver2000中使用存储过程创建表
- SQL Server:使用系统存储过程实现的通用分页存储过程
- SQL 使用存储过程创建报表的一点体会
- Datalist使用存储过程来分页
- 使用XML、XSLT和XPath创建可排序、分页、重用的数据显示页面
- 使用sqlserver2000存储过程分页小例