简单分页存储过程
代码
—–简单分页存储过程
—–2009.12.9
—–liuwm
create proc spPagedSelect
@table varchar(1000),—表名,可以是一个sql语句
@page int, —当前页面
@pageSize int, —分页大小
@key varchar(50), —表中的主键
@field varchar(50), —排序列名
@sort varchar(4) —排序方式 asc或desc
as
begin
declare @sqlstr varchar(5000)
set @sqlstr=\’select top \’+ cast(@pageSize as varchar(10)) +\’ * from \’ + @table +\’ where \’+ @key+\’ not in (select top \’ +cast((@pageSize*(@page–1)) as varchar(10))+\’ \’+@key +\’ from \’+@table +\’ order by \’+ @field +\’ \’+ @sort+ \’) order by \’+ @field +\’ \’+ @sort
print @sqlstr
exec (@sqlstr)
end —-使用方法如下–
exec spPagedSelect \’(select sum(amount) as sales,hotelid
from hhc_h_order
where year(adddate)=2009
group by hotelid) as t\’,1,3,\’hotelid\’,\’sales\’,\’desc\’
—注意上面的排序字段sales,是从sql语句后的sales取的,并非从物理表中取的
exec spPagedSelect \’jobs\’,1,3,\’job_id\’,\’avg(job_id)\’,\’asc\’
—–2009.12.9
—–liuwm
create proc spPagedSelect
@table varchar(1000),—表名,可以是一个sql语句
@page int, —当前页面
@pageSize int, —分页大小
@key varchar(50), —表中的主键
@field varchar(50), —排序列名
@sort varchar(4) —排序方式 asc或desc
as
begin
declare @sqlstr varchar(5000)
set @sqlstr=\’select top \’+ cast(@pageSize as varchar(10)) +\’ * from \’ + @table +\’ where \’+ @key+\’ not in (select top \’ +cast((@pageSize*(@page–1)) as varchar(10))+\’ \’+@key +\’ from \’+@table +\’ order by \’+ @field +\’ \’+ @sort+ \’) order by \’+ @field +\’ \’+ @sort
print @sqlstr
exec (@sqlstr)
end —-使用方法如下–
exec spPagedSelect \’(select sum(amount) as sales,hotelid
from hhc_h_order
where year(adddate)=2009
group by hotelid) as t\’,1,3,\’hotelid\’,\’sales\’,\’desc\’
—注意上面的排序字段sales,是从sql语句后的sales取的,并非从物理表中取的
exec spPagedSelect \’jobs\’,1,3,\’job_id\’,\’avg(job_id)\’,\’asc\’
版权声明:本文为wenming205原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。