万能存储过程分页
Posted yuqianwangnan
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了万能存储过程分页相关的知识,希望对你有一定的参考价值。
--通用分页显示存储过程
create proc p_paging
@tableName varchar(8000), --表名、视图名
@indexCol varchar(50) = ‘‘, --标识列名(如:比如主键、标识,推荐使用索引列)
@pageSize int = 10, --页面大小
@pageIndex int = 1, --当前页
@orderCol varchar(100) = ‘desc‘,--排序 (如:id)
@where varchar(max) = ‘1=1‘, --条件
@columns varchar(500) = ‘*‘ --要显示的列
as
declare @sql varchar(max)
declare @sql2 varchar(max)
if @where <> ‘‘
begin
select @sql2=‘select count(‘[email protected]+‘) from ‘[email protected]+‘ where ‘+ @where
select @sql=‘select ‘+ @columns+ ‘ from ‘+ @tableName+‘ where ‘+ @where +‘ order by ‘+ (@indexCol)+‘ ‘[email protected]+‘‘+ ‘ offset ‘ +Convert(varchar(10),((@pageIndex-1)*@pageSize))+ ‘ row fetch next ‘ + Convert(varchar(10),(@pageSize))+ ‘ row only‘
end
else
begin
select @sql2=‘select count(‘[email protected]+‘) from ‘[email protected]
select @sql2=‘select count(‘[email protected]+‘) from ‘[email protected]
select @sql=‘select ‘+ @columns+ ‘ from ‘+ @tableName+‘ order by ‘+ (@indexCol)+‘ ‘[email protected]+‘‘+ ‘ offset ‘ +Convert(varchar(10),((@pageIndex-1)*@pageSize))+ ‘ row fetch next ‘ + Convert(varchar(10),@pageSize)+ ‘ row only‘
end
以上是关于万能存储过程分页的主要内容,如果未能解决你的问题,请参考以下文章