本存储过程没有返回总记录数,但支持Ms sql 2000、2005
Create PROCEDURE dbo.pro_getListByPage
(
@tbname VARCHAR(30),--表名称
@fldname VARCHAR(20),--自动增加列
@column VARCHAR(200),--返回列
@pagesize INT,--每页显示条数
@pageindex INT,--当前页数
@where VARCHAR(300),--不带where的条件
@orderby VARCHAR(200),--排序字段
@orderbytype VARCHAR(5)--排序类型asc,desc
)
AS
DECLARE @sqlstr VARCHAR(2000)
DECLARE @strOrder VARCHAR(250)
DECLARE @strTmp VARCHAR(50)
---数据
IF @orderbytype = 'desc'
begin
SET @strOrder = ' order by '+@fldname+' desc'
set @strTmp ='<(select min'
end
ELSE
begin
SET @strOrder =' order by '+@fldname+' asc'
set @strTmp ='>(select max'
end
--当前为第一页
IF @pageindex = 1
SET @sqlstr = 'select top '+str(@pagesize) +' '+@column +' from ' +@tbname + ' where ' + @where +' order by ' + @orderby+' '+ @orderbytype
ELSE
set @sqlstr='select top '+str(@pagesize) +' '+@column+ ' from '+@tbname+' where [' +@fldname +']'
+@strTmp+'([' + @fldname +']) from (select top '+str((@pageindex-1)*@pagesize) +' ['+@fldname +'] from ['
+@tbname+'] where ' + @where + ' ' +@strOrder +') as tblTmp) and ' + @where+' ' + @strOrder
EXEC(@sqlstr)
(
@tbname VARCHAR(30),--表名称
@fldname VARCHAR(20),--自动增加列
@column VARCHAR(200),--返回列
@pagesize INT,--每页显示条数
@pageindex INT,--当前页数
@where VARCHAR(300),--不带where的条件
@orderby VARCHAR(200),--排序字段
@orderbytype VARCHAR(5)--排序类型asc,desc
)
AS
DECLARE @sqlstr VARCHAR(2000)
DECLARE @strOrder VARCHAR(250)
DECLARE @strTmp VARCHAR(50)
---数据
IF @orderbytype = 'desc'
begin
SET @strOrder = ' order by '+@fldname+' desc'
set @strTmp ='<(select min'
end
ELSE
begin
SET @strOrder =' order by '+@fldname+' asc'
set @strTmp ='>(select max'
end
--当前为第一页
IF @pageindex = 1
SET @sqlstr = 'select top '+str(@pagesize) +' '+@column +' from ' +@tbname + ' where ' + @where +' order by ' + @orderby+' '+ @orderbytype
ELSE
set @sqlstr='select top '+str(@pagesize) +' '+@column+ ' from '+@tbname+' where [' +@fldname +']'
+@strTmp+'([' + @fldname +']) from (select top '+str((@pageindex-1)*@pagesize) +' ['+@fldname +'] from ['
+@tbname+'] where ' + @where + ' ' +@strOrder +') as tblTmp) and ' + @where+' ' + @strOrder
EXEC(@sqlstr)
以下是适用于Ms Sql2005的存储过程分页,支持多表联合查询
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
GO
CREATE procedure [dbo].[sp_CommonPager]
(
--1,参数的括号可要可不要,有默认值的参数,在调用的时候,可以不写出来
--2,调用:
--declare @i int
--exec sp_CommonPager 'list','id,title','id',3,4,1,'classid=6',@i out
@tblName varchar(100), -- 表名
@fldCow varchar(300)='*', -- 要查询的列
@fldName varchar(255), -- 排序列
@PageSize int = 10, -- 页尺寸
@PageIndex int = 1, -- 页码
@OrderType int = 1, -- 设置排序类型, 1则降序
@strWhere varchar(200) = '', -- 查询条件 (注意: 不要加 where)
@count varchar(10) output --输出符合条件的记录的总数
)
AS
declare @strSQL varchar(1000); -- 主语句
declare @strOrder varchar(500) ; -- 排序类型
declare @strTmp varchar(100) ; --临时变量
declare @endIndex int; -- 结束的索引
declare @startIndex int; -- 开始的索引
declare @countSql nvarchar(500); --查询记录总数的SQL
--得到索引
set @startIndex=(@PageIndex-1)*@PageSize + 1;--注意,这里要加1
set @endIndex=@PageIndex*@PageSize;
--生成排序语句
--为了多表联合查询,这里要把表名字和排序字段的[]去掉-
if @OrderType != 0
set @strOrder = ' order by ' + @fldName + ' desc'
else
set @strOrder = ' order by ' + @fldName + ' asc'
set @strSQL = '(select top ' + ltrim(str(@endIndex)) +' '+@fldCow+','
+ 'row_number() over ('+ @strOrder +') as rownumber from '
+ @tblName + '' ;
set @countSql= 'select @count=count('+@fldName+') from '+ @tblName ;
if @strWhere! = ''
begin
set @strSQL =@strSQL+ ' where ('+ @strWhere + ') ';
set @countSql=@countSql + ' where ('+ @strWhere + ') ';
end
set @strSQL =@strSQL+ ') as tblTmp'
--得到记录总数
set @countSql=N'select @count=count(*) from ' + @tblName;
if @strWhere! = ''
set @countSql=@countSql+ N' where ' + @strWhere;
EXEC sp_executesql @countSql,N'@count varchar(20) out',@count out
set @strSQL = 'select * from ' + @strSQL + ' where rownumber between ' + ltrim(str(@startIndex)) + ' and '
+ ltrim(str(@endIndex));
--执行主语句
set nocount on -- 防止显示有关受影响的行数的信息
exec (@strSQL)
--print @strSQL
set QUOTED_IDENTIFIER ON
GO
CREATE procedure [dbo].[sp_CommonPager]
(
--1,参数的括号可要可不要,有默认值的参数,在调用的时候,可以不写出来
--2,调用:
--declare @i int
--exec sp_CommonPager 'list','id,title','id',3,4,1,'classid=6',@i out
@tblName varchar(100), -- 表名
@fldCow varchar(300)='*', -- 要查询的列
@fldName varchar(255), -- 排序列
@PageSize int = 10, -- 页尺寸
@PageIndex int = 1, -- 页码
@OrderType int = 1, -- 设置排序类型, 1则降序
@strWhere varchar(200) = '', -- 查询条件 (注意: 不要加 where)
@count varchar(10) output --输出符合条件的记录的总数
)
AS
declare @strSQL varchar(1000); -- 主语句
declare @strOrder varchar(500) ; -- 排序类型
declare @strTmp varchar(100) ; --临时变量
declare @endIndex int; -- 结束的索引
declare @startIndex int; -- 开始的索引
declare @countSql nvarchar(500); --查询记录总数的SQL
--得到索引
set @startIndex=(@PageIndex-1)*@PageSize + 1;--注意,这里要加1
set @endIndex=@PageIndex*@PageSize;
--生成排序语句
--为了多表联合查询,这里要把表名字和排序字段的[]去掉-
if @OrderType != 0
set @strOrder = ' order by ' + @fldName + ' desc'
else
set @strOrder = ' order by ' + @fldName + ' asc'
set @strSQL = '(select top ' + ltrim(str(@endIndex)) +' '+@fldCow+','
+ 'row_number() over ('+ @strOrder +') as rownumber from '
+ @tblName + '' ;
set @countSql= 'select @count=count('+@fldName+') from '+ @tblName ;
if @strWhere! = ''
begin
set @strSQL =@strSQL+ ' where ('+ @strWhere + ') ';
set @countSql=@countSql + ' where ('+ @strWhere + ') ';
end
set @strSQL =@strSQL+ ') as tblTmp'
--得到记录总数
set @countSql=N'select @count=count(*) from ' + @tblName;
if @strWhere! = ''
set @countSql=@countSql+ N' where ' + @strWhere;
EXEC sp_executesql @countSql,N'@count varchar(20) out',@count out
set @strSQL = 'select * from ' + @strSQL + ' where rownumber between ' + ltrim(str(@startIndex)) + ' and '
+ ltrim(str(@endIndex));
--执行主语句
set nocount on -- 防止显示有关受影响的行数的信息
exec (@strSQL)
--print @strSQL
在实际测试过程中(500万数据量)
使用第二种的时间比第一种略快。大概快在20毫秒左右
select * from (select top 100 *,ROW_NUMBER() over(order by id desc) as rownumber from
tablename
where 1=1
)as t where rownumber between 91 and 100
来源:https://www.cnblogs.com/zhxhdean/archive/2011/05/11/2043014.html