一个自己常用的分页存储过程

CREATE   proc Test_autoPagePaiXu
@tablekey varchar(100),
@tablename varchar(100),
@pageindex int,
@pagesize int,
@where varchar(5000),
@paixu varchar(1000)
as
declare @sql varchar(8000)

if(@paixu='')
begin
    set @paixu=@tablekey
end
set @sql='select top '+str(@pagesize)+' * from '+@tablename+ ' where 1=1 ' +@where+
 'and '+@tablekey+' not in (select top '+str(@pageindex*@pagesize)+' '+@tablekey+' from '+
@tablename  +' where 1=1 '+@where+' order by '+@paixu+') order by '+@paixu

set @sql=@sql+' ; select count(*) from '+@tablename+' where 1=1 '+@where

exec (@sql)
GO

用这个东西来源于网络,也来源于以前的公司。但是,以前别人怎么用的,我不清楚,跟据自己的需要做了修改

下面这个是对查寻出来的记录所写的:
--如果输出后须要用哪个字段排序,请用DataView的Sort进行排序
--author:王剑锋
--Email:[email protected]
CREATE proc AutoPagination   
@tableKey varchar(50),        --表中的主键或唯一字段
@tableName varchar(100),      --表名
@pageIndex int,        --当前多少页 
@pageSize int,        --一页多少条记录
@where varchar(1000)       --分页条件 
as
declare @sql varchar(5000)
set @sql='select top '+str(@pageSize)+' * from '+@tableName+' where '+@tableKey+' not in (select top '+
str(@pageIndex*@pageSize)+' '+@tableKey+' from '+@tableName+' where 1=1 '+@where+' order by '+@tableKey+
' ) and 1=1 '+@where+' order by '+@tableKey +' ; select count(*) from '+@tableName+' where 1=1 '+@where

exec(@sql)
GO

你可能感兴趣的:(存储过程)