create procedure [dbo].[proc_datapages]
@tablenames varchar(200), --表名,可以是多个表,但不能用别名
@primarykey varchar(100), --主键,可以为空,但@order为空时该值不能为空
@fields varchar(800), --要取出的字段,可以是多个表的字段,可以为空,为空表示select *
@pagesize int, --每页记录数
@currentpage int, --当前页,0表示第1页
@filter varchar(200) = '', --条件,可以为空,不用填 where
@order varchar(200) = '', --排序,可以为空,为空默认按主键升序排列,不用填 order by
@recountcount int=0 output
as
begin
/*
declare @recountcount int
exec [proc_datapages]
@tablenames='de_dict',
@primarykey='',
@fields='*',
@pagesize=10,
@currentpage=1,
@filter='',
@order='dictorder desc',
@recountcount=@recountcount output
print @recountcount
*/
set nocount on;
declare @toprow varchar(12)
declare @temppagesize varchar(12)
if(len(@order)>0)
begin
set @order=' order by
end
else
begin
set @order=' order by
end
if (len(@filter)<1)
begin
set @filter=' 1=1'
end
if(@currentpage-1<=0)
set @currentpage=0
set @toprow= rtrim(ltrim(str(@pagesize*(@currentpage-1))))
set @temppagesize= rtrim(ltrim(str(@pagesize)))
exec('set rowcount * from (select row_number() over () rownumber,' @fields ' from where ) temptable where rownumber>' @toprow 'set rowcount 0')
/*计算总页数*/
declare @sql nvarchar(max)
set @sql='select @recountcount=count(1) from where
set @recountcount=0
exec sp_executesql @sql,n'@recountcount int output',@recountcount=@recountcount output
set nocount off;
end
posted on 2014-03-17 15:32
sanmao 阅读(723)
评论(0) 编辑 收藏