复制代码 代码如下:
—————————————————-
–针对sql 2000 的分页存储过程
–time:2008-9-25
—————————————————-
alter procedure [dbo].[uosp_recordpager]
–要分页显示的表名,可多表连查,但不能使用别名。
–例:uo_article left join uo_articleclass on uo_article.aclassid=uo_articleclass.id
@table_info varchar(100),
–用于定位记录的主键(惟一键)字段,可以是逗号分隔的多个字段
@field_id nvarchar(1000),
–要显示的页码
@currentpage int=1,
–每页的大小(记录数)
@pagesize int=10,
–以逗号分隔的要显示的字段列表,如果不指定,则为*。但多表联查时,若有同名字段,则必须显示指定要查的字段。
–例:uo_article.*,uo_articleclass.classname
@field_info nvarchar(1000),
–以逗号分隔的排序字段列表,可以指定在字段后面指定desc/asc 用于指定排序顺序
@field_order nvarchar(1000)= ‘ ‘,
–查询条件
@otherwhere nvarchar(1000)= ‘ ‘,
@recordcount int output, –总行数
@pagecount int output, –总页数
@sqlstr nvarchar(2000) output
as
set nocount on
–分页字段检查
if isnull(@field_id,n’ ‘)= ‘ ‘
begin
raiserror(n’分页处理需要主键(或者惟一键) ‘,1,16)
return
end
–其他参数检查及规范
if isnull(@pagesize,0) <1 set @pagesize=10
if isnull(@field_info,n’ ‘)=n’ ‘ set @field_info=n’* ‘
if isnull(@field_order,n’ ‘)=n’ ‘
set @field_order=n’ ‘
else
set @field_order=n’order by ‘+ltrim(@field_order)
if isnull(@otherwhere,n’ ‘)=n’ ‘
set @otherwhere=n’ ‘
else
set @otherwhere=n’ where (‘+@otherwhere+n’) ‘
–计算@recordcount
declare @sqlt nvarchar(1000)
set @sqlt = ‘select @recordcount = count(‘ + @field_id + ‘) from ‘ + @table_info + @otherwhere
exec sp_executesql @sqlt,n’@recordcount int output’,@recordcount output
–如果@pagecount为null值,则计算总页数(这样设计可以只在第一次计算总页数,以后调用时,把总页数传回给存储过程,避免再次计算总页数,对于不想计算总页数的处理而言,可以给@pagecount赋值)
if @pagecount is null
begin
declare @sql nvarchar(4000)
set @sql=n’select @pagecount=count(*) ‘
+n’ from ‘+@table_info
+n’ ‘+@otherwhere
exec sp_executesql @sql,n’@pagecount int output ‘,@pagecount output
set @pagecount=(@pagecount+@pagesize-1)/@pagesize
end
if isnull(@currentpage,0) <1 set @currentpage=1
if @currentpage > @pagecount and @pagecount>0 set @currentpage=@pagecount
–第一页直接显示
if @currentpage = 1
begin
set @sqlstr = n’select top ‘ + str(@pagesize) +n’ ‘+@field_info +n’ from ‘+@table_info +n’ ‘+@otherwhere +n’ ‘+@field_order
–print @sqlstr
exec(@sqlstr)
end
else
begin
—————————————————-
–获取第一个表的表名
declare @firsttablename varchar(20)
set @firsttablename = @table_info
if charindex(n’,’,@firsttablename)>0 or charindex(n’.’,@firsttablename)> 0 or charindex(n’ ‘,@firsttablename)> 0
begin
while charindex(n’,’,@firsttablename)> 0
select @firsttablename=left(@firsttablename,charindex(n’,’,@firsttablename)-1)
while charindex(n’.’,@firsttablename)> 0
select @firsttablename=left(@firsttablename,charindex(n’.’,@firsttablename)-1)
while charindex(n’ ‘,@firsttablename)> 0
select @firsttablename=left(@firsttablename,charindex(n’ ‘,@firsttablename)-1)
end
–构造sql语句
set @sqlstr = n’select * from (‘
+ n’ select top ‘+str(@pagesize*@currentpage) +n’ ‘+ @field_info + n’ from ‘+@table_info +n’ ‘+@otherwhere +n’ ‘+@field_order
+ n’) ‘+@firsttablename+n’ where ‘+@field_id+n’ not in (‘
+ n’ select top ‘+str(@pagesize*(@currentpage-1))+n’ ‘+ @field_id + n’ from ‘+@table_info +n’ ‘+@otherwhere +n’ ‘+@field_order
+ n’) ‘
+ n’ ‘+@field_order
exec(@sqlstr)
—————————————————-
end