T-sql 存储过程的分页
2010-09-03 11:09
429 查看
use pubs
go
if exists(select name from sysobjects where name = 'emp')
drop table emp
go
--复制表
select emp_id,fname,lname into emp from dbo.employee
--select * from emp
--例如,求第3页,每页10条
--即第21到30条记录
select top 10 * from emp
where emp_id not in(select top 20 emp_id from emp order by emp_id)
order by emp_id
--分页存储过程
if exists(select name from sysobjects where name = 'up_pager' and type ='p')
drop procedure up_pager
go
create procedure up_pager
@pageSize int, --每页显示记录数
@pageIndex int, --当前页码
@tableName varchar(30), --表名
@colName varchar(30) --排序的字段名
as
begin
declare @sqlStr varchar(300)
if @pageIndex = 1
set @sqlStr = ' select top '+cast(@pageSize as varchar(20))+' * from '+@tableName+
' order by '+@colName
else
set @sqlStr = ' select top '+cast(@pageSize as varchar(20))+' * from '+@tableName+
' where '+@colName+' not in '+
'(select top '+ cast((@pageIndex-1)*@pageSize as varchar(20)) +' '+@colName+' from '+@tableName+
' order by '+@colName+')'+
' order by '+@colName
--print @sqlStr
execute(@sqlStr)
end
go
execute up_pager 10,1,'emp','emp_id'
--例如求总记录数和总页数
select count(*) as 'rowcount','pageCount' = case
when count(*)% 10 = 0 then (count(*)/10)
when count(*)% 10 > 0 then (count(*)/10+1)
end
from emp
--求总记录数和总页数
if exists(select name from sysobjects where name = 'up_pagercount' and type ='p')
drop procedure up_pagercount
go
create procedure up_pagercount
@pageSize int, --每页显示记录数
@tableName varchar(30) --表名
as
begin
declare @sqlStr varchar(300)
set @sqlStr = 'select count(*) as ''RowCount'', pageCount = ' + ' case ' +
' when count(*)%'+cast(@pageSize as varchar(20)) +'=0 then count(*)/'+cast(@pageSize as varchar(20))+
' when count(*)%'+cast(@pageSize as varchar(20)) +'>0 then (count(*)/'+cast(@pageSize as varchar(20))+'+1)'+
' end' + ' from '+ @tableName
execute (@sqlStr)
end
go
execute up_pagercount 10,'emp'
go
if exists(select name from sysobjects where name = 'emp')
drop table emp
go
--复制表
select emp_id,fname,lname into emp from dbo.employee
--select * from emp
--例如,求第3页,每页10条
--即第21到30条记录
select top 10 * from emp
where emp_id not in(select top 20 emp_id from emp order by emp_id)
order by emp_id
--分页存储过程
if exists(select name from sysobjects where name = 'up_pager' and type ='p')
drop procedure up_pager
go
create procedure up_pager
@pageSize int, --每页显示记录数
@pageIndex int, --当前页码
@tableName varchar(30), --表名
@colName varchar(30) --排序的字段名
as
begin
declare @sqlStr varchar(300)
if @pageIndex = 1
set @sqlStr = ' select top '+cast(@pageSize as varchar(20))+' * from '+@tableName+
' order by '+@colName
else
set @sqlStr = ' select top '+cast(@pageSize as varchar(20))+' * from '+@tableName+
' where '+@colName+' not in '+
'(select top '+ cast((@pageIndex-1)*@pageSize as varchar(20)) +' '+@colName+' from '+@tableName+
' order by '+@colName+')'+
' order by '+@colName
--print @sqlStr
execute(@sqlStr)
end
go
execute up_pager 10,1,'emp','emp_id'
--例如求总记录数和总页数
select count(*) as 'rowcount','pageCount' = case
when count(*)% 10 = 0 then (count(*)/10)
when count(*)% 10 > 0 then (count(*)/10+1)
end
from emp
--求总记录数和总页数
if exists(select name from sysobjects where name = 'up_pagercount' and type ='p')
drop procedure up_pagercount
go
create procedure up_pagercount
@pageSize int, --每页显示记录数
@tableName varchar(30) --表名
as
begin
declare @sqlStr varchar(300)
set @sqlStr = 'select count(*) as ''RowCount'', pageCount = ' + ' case ' +
' when count(*)%'+cast(@pageSize as varchar(20)) +'=0 then count(*)/'+cast(@pageSize as varchar(20))+
' when count(*)%'+cast(@pageSize as varchar(20)) +'>0 then (count(*)/'+cast(@pageSize as varchar(20))+'+1)'+
' end' + ' from '+ @tableName
execute (@sqlStr)
end
go
execute up_pagercount 10,'emp'
相关文章推荐
- SQL分页的存储过程详解
- SQL 实现随机分页的通用分页存储过程
- [MSSQL]SQL通用存储过程分页,支持多表联合
- MS SQLSERVER通用存储过程分页
- sql 存储过程千万条数据分页
- SQL 分割字符串和通用分页查询存储过程
- sql 存储过程分页
- 一个项目中正在使用的SQL分页存储过程
- 三种SQL分页查询的存储过程代码
- 分页存储过程sql2005以上版本
- 简单的SQL存储过程分页
- sql分页存储过程(二)
- 存储过程分页实现代码,2005 T-SQL新增功能
- 关于SQL存储过程分页的研究
- 数据库开发必看 -- 用索引优化SQL,开发优秀的分页存储过程(二)
- sql分页存储过程!与asp.net分页控件一起使用
- SQL2005 存储过程通用分页
- Sql存储过程几种分页方式
- sql 可按任意字段排序的分页存储过程(转)