分页存储过程:分页存储过程代码

复制代码 代码如下:

/*
*@curentpage 当前页
*@pagesize 每页记录数
*@TableName 表名
*@key 主键(自动排序)
*@where 查询条件
1)空为 null
2)有查询条件不要带where
*@order '0'表示 desc '1'是asc
*@pageCount 总页数
*/
create procedure Page
@currentpage ,@pagesize ,
@TableName varchar(30),@key varchar(30),
@where varchar(50),@order varchar(1),
@pageCount ,@str varchar(450) output
as
begin
---------------执行sql语句------------
declare @sql nvarchar(400),@ordreby nvarchar(200)
declare @tempsql1 varchar(200),@tempsql2 varchar(200)
---------------记录总数-----------------
declare @count
---------------临时变量------------------------
declare @temp1 ,@temp2
@TableName=' '+@TableName+' '
@key=' '+@key+' '
@order='0'
@ordreby=' order by '+@key+'desc'

@ordreby=' order by '+@key
@where='null'
@sql='select @count = count(*) from '+ @TableName

@sql='select @count = count(*) from '+ @TableName+' where '+@where
------------@count 付值(声明变量@count 在介绍说明是output 内型)---------------------------
exec sp_executesql @sql,N'@count out',@count out
------------求总页数------------------------------
(@count%@pagesize)=0
@pagecount=@count/@pagesize

@pagecount=@count/@pagesize+1
-----------判断显示当前页是否异常------------------
@currentpage>@pagecount
@currentpage=@pagecount
@currentpage<1
@currentpage=1
----------记录数小于页面显示记录数-----------------
(@currentpage=1)
begin
@where='null'
@where=' '

@where=' where '+@where
@sql = 'select top'+ str(@pagesize)+' * from '+@TableName+@where+@ordreby
end

begin
/**//* ---------------desc----------------------
*@temp1表示前面记录
*@temp2表示后面记录
*假设共77个记录每次取10个取67~58(第2页)去掉前面57(1~57)个和后面10个(77~66)
*/
@order=0
begin
@temp1 = @count-@currentpage*@pagesize
@temp1<0
@temp1=0
@temp2 = (@currentpage - 1)*@pagesize
@where='null'
begin
@tempsql1='select top ' + str(@temp1)+' '+@key+' from ' + @TableName+' order by ' +@key
@tempsql2='select top ' + str(@temp2)+' '+@key+' from ' + @TableName + @ordreby
end

begin
@tempsql1='select top ' + str(@temp1)+' '+@key+' from ' + @TableName+' where '+@where+' order by ' +@key
@tempsql2='select top ' + str(@temp2)+' '+@key+' from ' + @TableName+' where '+@where+@ordreby
end
@sql=' select top ' + str(@pagesize) + ' * from ' + @TableName + ' where '+@key+ ' not in '
@sql= @sql+' ( '+ @tempsql1 +' ) and '
@sql= @sql+@key+ ' not in ( '+@tempsql2 +' ) '
@where='null'
@sql= @sql+@ordreby

@sql= @sql+' and '+@where+@ordreby
end
/**//* ----------------asc---------------------
* @temp 表示前面显示记录总数
* 去掉 @temp 在取出 pagesize 个即可
*/

begin
@temp1=(@currentpage-1)*@pagesize
@where='null'
@tempsql1='select top '+ str(@temp1)+' '+@key+' from ' + @TableName + @ordreby

@tempsql1='select top '+ str(@temp1)+' '+@key+' from ' + @TableName ' where '+@where+@ordreby
@sql=' select top ' + str(@pagesize) + ' * from ' + @TableName + ' where '+@key+ ' not in '
@sql=@sql+' ( '+@tempsql1+' ) '
@where='null'
@sql= @sql+@ordreby

@sql= @sql+' and '+@where+@ordreby
end
/**//* -------------------------------------*/
end
@str=@sql
--exec sp_executesql @sql
end
GO

Tags:  sql分页存储过程 分页的存储过程 oracle分页存储过程 分页存储过程

延伸阅读

最新评论

发表评论