Sql万能分页代码

Posted 啊飞达

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Sql万能分页代码相关的知识,希望对你有一定的参考价值。

go  ----万能分页代码
create procedure [dbo].[sp_datapager]
@pagesize int,--每一页的大小
@pageindex int,--页码数
@tablename varchar(Max),--表的名称
@keycolumn varchar(20),---主键id
@columns varchar(200),--要查询出列的名称
@where varchar(200),---查询条件
@orderby varchar(100),---排序方式
@recordcount int out--输出参数,非0则返回要查询表的总记录数

as
declare @sql nvarchar(3000)
declare @rcsql nvarchar(1000)
set @rcsql=‘select @rc=count(*) from ‘[email protected]
set @sql=‘select top ‘+convert(varchar(3),@pagesize)+‘ ‘+ @columns+‘ from ‘+
@tablename +‘ where ‘[email protected]+‘ not in(select top ‘+
convert(varchar(10),(@pageindex-1)*@pagesize)+‘ ‘[email protected]+
‘ from ‘[email protected]+‘)‘
if (@where!=‘‘)
begin
set @rcsql=‘select @rc=count(*) from ‘[email protected]+‘ where ‘[email protected]
set @sql=‘select top ‘+convert(varchar(3),@pagesize)+ @columns+‘ from ‘+
@tablename +‘ where ‘[email protected]+‘ not in(select top ‘+
convert(varchar(10),(@pageindex-1)*@pagesize)+‘ ‘[email protected]+
‘ from ‘[email protected]+‘ where ‘[email protected]+‘) and ‘[email protected]
end
if (@orderby!=‘‘)
begin
if (@where!=‘‘)
begin
set @sql=‘select top ‘+convert(varchar(3),@pagesize)+ @columns+‘ from ‘+
@tablename +‘ where ‘[email protected]+‘ not in(select top ‘+
convert(varchar(10),(@pageindex-1)*@pagesize)+‘ ‘[email protected]+
‘ from ‘[email protected]+‘ where ‘[email protected]+‘ order by ‘[email protected]+‘) and ‘+
@where+‘ order by ‘ [email protected]
end
else
begin
set @sql=‘select top ‘+convert(varchar(3),@pagesize)+ @columns+‘ from ‘+
@tablename +‘ where ‘[email protected]+‘ not in(select top ‘+
convert(varchar(10),(@pageindex-1)*@pagesize)+‘ ‘[email protected]+
‘ from ‘[email protected]+‘ order by ‘[email protected]+‘)‘+‘ order by ‘ [email protected]
end

end
declare @param nvarchar(100)
set @param=‘@rc int output‘
exec sp_executesql @sql
exec sp_executesql @rcsql,@param,@[email protected] output

















































以上是关于Sql万能分页代码的主要内容,如果未能解决你的问题,请参考以下文章

PHP+mysql分页的问题,我用了php万能分页的代码,但是不好用,请帮我看看是啥问题?说19行不对,请指教

万能存储过程分页

python中的万能分页

存储过程万能分页

SQL 分页代码

SQL 分页代码