码迷,mamicode.com
首页 > 数据库 > 详细

数据库分页全集

时间:2015-02-06 12:43:45      阅读:176      评论:0      收藏:0      [点我收藏+]

标签:

use master go if DB_ID(‘Info‘) is not null drop database Info go create database Info go use Info go if DB_ID(‘biao‘) is not null drop database biao go create table biao ( id int identity primary key, name varchar(50), Time datetime ) --插入一百万测试数据 declare @id int set @id=1 while(@id<=1000000) begin insert into biao values(‘ad‘,GETDATE()) set @id+=1 end go select *from biao go --定位法 (利用ID大于多少)每页显示 10条数据 select top 10* from biao where (id >(select max(id) from (select top 100000 id from biao order by id) as T)) order by id go  -- 用row_number()分页(1) with T as ( select top 30 *,row_number() over (order by biao.id) as pos from biao ) select * from T where T.pos>=1 and T.pos<=30 go --用row_number()分页(2) select * from ( select *,row_number() over (order by id) as rank from biao ) as t where t.rank between 100001 and 100010 go --利用Not In) select top 10* from biao where id not in ( select top 100000 id from biao order by id ) order by id go --(利用IN) select top 10 * from biao where id in( select top 10 id from( select top 100010 id from biao order by id ) as t order by t.id desc ) order by id --分页 declare @SortColumn varchar(40) --即 top 100001,取出最大的 id覆盖@SortColumn set rowcount 100001 select @SortColumn= id from biao order by id --即 top 10 set rowcount 10 select * from biao where id >= @SortColumn order by id go --分页 select top 10 * from biao where id in ( select top 10 id from ( select top 100010 id from biao order by id ) as t order by t.id desc ) go --分页 declare @pagesize int,@pageNum int set @pagesize=10 set @pageNum=100000 select * from( (select row_number() over(order by id) id,name,Time from biao ) )tb where id>@pagesize*(@pageNum-1) and id<=@pagesize*(@pageNum) order by id go --千万级分页存储过程 --SET ANSI_NULLS ON --GO --SET QUOTED_IDENTIFIER ON --GO --分页存储过程 --CREATE PROCEDURE [dbo].[sp_Paging] --( --@Tables nvarchar(1000), --表名/视图名 --@PrimaryKey nvarchar(100), --主键 --@Sort nvarchar(200) = NULL, --排序字段(不带order by) --@pageindex int = 1, --当前页码 --@PageSize int = 10, --每页记录数 --@Fields nvarchar(1000) = N‘*‘, --输出字段 --@Filter nvarchar(1000) = NULL, --where过滤条件(不带where) --@Group nvarchar(1000) = NULL, --Group语句(不带Group By) --@TotalCount int OUTPUT --总记录数 --) --AS --DECLARE @SortTable nvarchar(100) --DECLARE @SortName nvarchar(100) --DECLARE @strSortColumn nvarchar(200) --DECLARE @operator char(2) --DECLARE @type nvarchar(100) --DECLARE @prec int --设定排序语句 --IF @Sort IS NULL OR @Sort = ‘‘ -- SET @Sort = @PrimaryKey --IF CHARINDEX(‘DESC‘,@Sort)>0 --BEGIN -- SET @strSortColumn = REPLACE(@Sort, ‘DESC‘, ‘‘) -- SET @operator = ‘<=‘ --END --ELSE --BEGIN -- SET @strSortColumn = REPLACE(@Sort, ‘ASC‘, ‘‘) -- SET @operator = ‘>=‘ --END --IF CHARINDEX(‘.‘, @strSortColumn) > 0 --BEGIN -- SET @SortTable = SUBSTRING(@strSortColumn, 0, CHARINDEX(‘.‘,@strSortColumn)) -- SET @SortName = SUBSTRING(@strSortColumn, CHARINDEX(‘.‘,@strSortColumn) + 1, LEN(@strSortColumn)) --END --ELSE --BEGIN -- SET @SortTable = @Tables -- SET @SortName = @strSortColumn --END --设置排序字段类型和精度 --SELECT @type=t.name, @prec=c.prec FROM sysobjects o -- JOIN syscolumns c on o.id=c.id -- JOIN systypes t on c.xusertype=t.xusertype WHERE o.name = @SortTable AND c.name = @SortName --IF CHARINDEX(‘char‘, @type) > 0 -- SET @type = @type + ‘(‘ + CAST(@prec AS varchar) + ‘)‘ --DECLARE @strPageSize nvarchar(50) --DECLARE @strStartRow nvarchar(50) --DECLARE @strFilter nvarchar(1000) --DECLARE @strSimpleFilter nvarchar(1000) --DECLARE @strGroup nvarchar(1000) --IF @pageindex <1 -- SET @pageindex = 1 --SET @strPageSize = CAST(@PageSize AS nvarchar(50)) --设置开始分页记录数 --SET @strStartRow = CAST(((@pageindex - 1)*@PageSize + 1) AS nvarchar(50)) --筛选以及分组语句 --IF @Filter IS NOT NULL AND @Filter != ‘‘ --BEGIN -- SET @strFilter = ‘ WHERE ‘ + @Filter + ‘ ‘ -- SET @strSimpleFilter = ‘ AND ‘ + @Filter + ‘ ‘ --END --ELSE --BEGIN -- SET @strSimpleFilter = ‘‘ -- SET @strFilter = ‘‘ --END --IF @Group IS NOT NULL AND @Group != ‘‘ -- SET @strGroup = ‘ GROUP BY ‘ --计算总记录数 --DECLARE @TotalCountSql nvarchar(1000) --SET @TotalCountSql=N‘SELECT @TotalCount=COUNT(*)‘ +N‘ FROM ‘ + @Tables + @strFilter --EXEC sp_executesql @TotalCountSql,N‘@TotalCount int OUTPUT‘,@TotalCount OUTPUT --执行查询语句 --EXEC( --‘ --DECLARE @SortColumn ‘ + @type + ‘ --SET ROWCOUNT ‘ + @strStartRow + ‘ --SELECT @SortColumn=‘ + @strSortColumn + ‘ FROM ‘ + @Tables + @strFilter + ‘ ‘ + @strGroup + ‘ ORDER BY ‘ + @Sort + ‘ --SET ROWCOUNT ‘ + @strPageSize + ‘ --SELECT ‘ + @Fields + ‘ FROM ‘ + @Tables + ‘ WHERE ‘ + @strSortColumn + @operator + ‘ @SortColumn ‘ + @strSimpleFilter + ‘ ‘ + @strGroup + ‘ ORDER BY ‘ + @Sort + ‘ --‘ --) -----------利用select top and select max( --create procedure proc_paged_with_selectMax --利用select top and select max(列) --( -- @pageIndex int, --页索引 -- @pageSize int --页记录数 --) --as --begin --set nocount on; -- declare @timediff datetime -- declare @sql nvarchar(500) -- select @timediff=Getdate() -- set @sql=‘select top ‘+str(@pageSize)+‘ * From tb_TestTable where(ID>(select max(id) From (select top ‘+str(@pageSize*@pageIndex)+‘ id From tb_TestTable order by ID) as TempTable)) order by ID‘ -- execute(@sql) -- select datediff(ms,@timediff,GetDate()) as 耗时 --set nocount off; --end --go ----/*-----存储过程 分页处理 孙伟 2005-03-28创建 -------*/ ----/*-----存储过程 分页处理 浪尘 2008-9-1修改----------*/ ----/*----- 对数据进行了2分处理使查询前半部分数据与查询后半部分数据性能相同 -------*/ --alter PROCEDURE proc_paged_2part_selectMax --( --@tblName nvarchar(200), ----要显示的表或多个表的连接 --@fldName nvarchar(500) = ‘*‘, ----要显示的字段列表 --@pageSize int = 10, ----每页显示的记录个数 --@page int = 1, ----要显示那一页的记录 --@fldSort nvarchar(200) = null, ----排序字段列表或条件 --@Sort bit = 0, ----排序方法,0为升序,1为降序(如果是多字段排列Sort指代最后一个排序字段的排列顺序(最后一个排序字段不加排序标记)--程序传参如:‘ SortA Asc,SortB Desc,SortC ‘) --@strCondition nvarchar(1000) = null, ----查询条件,不需where --@ID nvarchar(150), ----主表的主键 --@Dist bit = 0, ----是否添加查询字段的 DISTINCT 默认0不添加/1添加 --@pageCount int = 1 output, ----查询结果分页后的总页数 --@Counts int = 1 output ----查询到的记录数 --) --AS --SET NOCOUNT ON --Declare @sqlTmp nvarchar(1000) ----存放动态生成的SQL语句 --Declare @strTmp nvarchar(1000) ----存放取得查询结果总数的查询语句 --Declare @strID nvarchar(1000) ----存放取得查询开头或结尾ID的查询语句 --Declare @strSortType nvarchar(10) ----数据排序规则A --Declare @strFSortType nvarchar(10) ----数据排序规则B --Declare @SqlSelect nvarchar(50) ----对含有DISTINCT的查询进行SQL构造 --Declare @SqlCounts nvarchar(50) ----对含有DISTINCT的总数查询进行SQL构造 --declare @timediff datetime --耗时测试时间差 --select @timediff=getdate() --if @Dist = 0 --begin -- set @SqlSelect = ‘select ‘ -- set @SqlCounts = ‘Count(*)‘ --end --else --begin -- set @SqlSelect = ‘select distinct ‘ -- set @SqlCounts = ‘Count(DISTINCT ‘+@ID+‘)‘ --end --if @Sort=0 --begin -- set @strFSortType=‘ ASC ‘ -- set @strSortType=‘ DESC ‘ --end --else --begin -- set @strFSortType=‘ DESC ‘ -- set @strSortType=‘ ASC ‘ --end ----------生成查询语句-------- ----此处@strTmp为取得查询结果数量的语句 --if @strCondition is null or @strCondition=‘‘ --没有设置显示条件 --begin -- set @sqlTmp = @fldName + ‘ From ‘ + @tblName -- set @strTmp = @SqlSelect+‘ @Counts=‘+@SqlCounts+‘ FROM ‘+@tblName -- set @strID = ‘ From ‘ + @tblName --end --else --begin -- set @sqlTmp = + @fldName + ‘From ‘ + @tblName + ‘ where (1>0) ‘ + @strCondition -- set @strTmp = @SqlSelect+‘ @Counts=‘+@SqlCounts+‘ FROM ‘+@tblName + ‘ where (1>0) ‘ + @strCondition -- set @strID = ‘ From ‘ + @tblName + ‘ where (1>0) ‘ + @strCondition --end ------取得查询结果总数量----- --exec sp_executesql @strTmp,N‘@Counts int out ‘,@Counts out --declare @tmpCounts int --if @Counts = 0 -- set @tmpCounts = 1 --else -- set @tmpCounts = @Counts -- --取得分页总数 -- set @pageCount=(@tmpCounts+@pageSize-1)/@pageSize -- /**//**//**//**当前页大于总页数 取最后一页**/ -- if @page>@pageCount -- set @page=@pageCount -- --/*-----数据分页2分处理-------*/ -- declare @pageIndex int --总数/页大小 -- declare @lastcount int --总数%页大小 -- set @pageIndex = @tmpCounts/@pageSize -- set @lastcount = @tmpCounts%@pageSize -- if @lastcount > 0 -- set @pageIndex = @pageIndex + 1 -- else -- set @lastcount = @pagesize -- --//***显示分页 -- if @strCondition is null or @strCondition=‘‘ --没有设置显示条件 -- begin -- if @pageIndex<2 or @page<=@pageIndex / 2 + @pageIndex % 2 --前半部分数据处理 -- begin -- if @page=1 -- set @strTmp=@SqlSelect+‘ top ‘+ CAST(@pageSize as VARCHAR(4))+‘ ‘+ @fldName+‘ from ‘+@tblName -- +‘ order by ‘+ @fldSort +‘ ‘+ @strFSortType -- else -- begin -- if @Sort=1 -- begin -- set @strTmp=@SqlSelect+‘ top ‘+ CAST(@pageSize as VARCHAR(4))+‘ ‘+ @fldName+‘ from ‘+@tblName -- +‘ where ‘+@ID+‘ <(select min(‘+ @ID +‘) from (‘+ @SqlSelect+‘ top ‘+ CAST(@pageSize*(@page-1) as Varchar(20)) +‘ ‘+ @ID +‘ from ‘+@tblName -- +‘ order by ‘+ @fldSort +‘ ‘+ @strFSortType+‘) AS TBMinID)‘ -- +‘ order by ‘+ @fldSort +‘ ‘+ @strFSortType -- end -- else -- begin -- set @strTmp=@SqlSelect+‘ top ‘+ CAST(@pageSize as VARCHAR(4))+‘ ‘+ @fldName+‘ from ‘+@tblName -- +‘ where ‘+@ID+‘ >(select max(‘+ @ID +‘) from (‘+ @SqlSelect+‘ top ‘+ CAST(@pageSize*(@page-1) as Varchar(20)) +‘ ‘+ @ID +‘ from ‘+@tblName -- +‘ order by ‘+ @fldSort +‘ ‘+ @strFSortType+‘) AS TBMinID)‘ -- +‘ order by ‘+ @fldSort +‘ ‘+ @strFSortType -- end -- end -- end -- else -- begin -- set @page = @pageIndex-@page+1 --后半部分数据处理 -- if @page <= 1 --最后一页数据显示 -- set @strTmp=@SqlSelect+‘ * from (‘+@SqlSelect+‘ top ‘+ CAST(@lastcount as VARCHAR(4))+‘ ‘+ @fldName+‘ from ‘+@tblName -- +‘ order by ‘+ @fldSort +‘ ‘+ @strSortType+‘) AS TempTB‘+‘ order by ‘+ @fldSort +‘ ‘+ @strFSortType -- else -- if @Sort=1 -- begin -- set @strTmp=@SqlSelect+‘ * from (‘+@SqlSelect+‘ top ‘+ CAST(@pageSize as VARCHAR(4))+‘ ‘+ @fldName+‘ from ‘+@tblName -- +‘ where ‘+@ID+‘ >(select max(‘+ @ID +‘) from(‘+ @SqlSelect+‘ top ‘+ CAST(@pageSize*(@page-2)+@lastcount as Varchar(20)) +‘ ‘+ @ID +‘ from ‘+@tblName -- +‘ order by ‘+ @fldSort +‘ ‘+ @strSortType+‘) AS TBMaxID)‘ -- +‘ order by ‘+ @fldSort +‘ ‘+ @strSortType+‘) AS TempTB‘+‘ order by ‘+ @fldSort +‘ ‘+ @strFSortType -- end -- else -- begin -- set @strTmp=@SqlSelect+‘ * from (‘+@SqlSelect+‘ top ‘+ CAST(@pageSize as VARCHAR(4))+‘ ‘+ @fldName+‘ from ‘+@tblName -- +‘ where ‘+@ID+‘ <(select min(‘+ @ID +‘) from(‘+ @SqlSelect+‘ top ‘+ CAST(@pageSize*(@page-2)+@lastcount as Varchar(20)) +‘ ‘+ @ID +‘ from ‘+@tblName -- +‘ order by ‘+ @fldSort +‘ ‘+ @strSortType+‘) AS TBMaxID)‘ -- +‘ order by ‘+ @fldSort +‘ ‘+ @strSortType+‘) AS TempTB‘+‘ order by ‘+ @fldSort +‘ ‘+ @strFSortType -- end -- end -- end -- else --有查询条件 -- begin -- if @pageIndex<2 or @page<=@pageIndex / 2 + @pageIndex % 2 --前半部分数据处理 -- begin -- if @page=1 -- set @strTmp=@SqlSelect+‘ top ‘+ CAST(@pageSize as VARCHAR(4))+‘ ‘+ @fldName+‘ from ‘+@tblName -- +‘ where 1=1 ‘ + @strCondition + ‘ order by ‘+ @fldSort +‘ ‘+ @strFSortType -- else if(@Sort=1) -- begin -- set @strTmp=@SqlSelect+‘ top ‘+ CAST(@pageSize as VARCHAR(4))+‘ ‘+ @fldName+‘ from ‘+@tblName -- +‘ where ‘+@ID+‘ <(select min(‘+ @ID +‘) from (‘+ @SqlSelect+‘ top ‘+ CAST(@pageSize*(@page-1) as Varchar(20)) +‘ ‘+ @ID +‘ from ‘+@tblName -- +‘ where (1=1) ‘ + @strCondition +‘ order by ‘+ @fldSort +‘ ‘+ @strFSortType+‘) AS TBMinID)‘ -- +‘ ‘+ @strCondition +‘ order by ‘+ @fldSort +‘ ‘+ @strFSortType -- end -- else -- begin -- set @strTmp=@SqlSelect+‘ top ‘+ CAST(@pageSize as VARCHAR(4))+‘ ‘+ @fldName+‘ from ‘+@tblName -- +‘ where ‘+@ID+‘ >(select max(‘+ @ID +‘) from (‘+ @SqlSelect+‘ top ‘+ CAST(@pageSize*(@page-1) as Varchar(20)) +‘ ‘+ @ID +‘ from ‘+@tblName -- +‘ where (1=1) ‘ + @strCondition +‘ order by ‘+ @fldSort +‘ ‘+ @strFSortType+‘) AS TBMinID)‘ -- +‘ ‘+ @strCondition +‘ order by ‘+ @fldSort +‘ ‘+ @strFSortType -- end -- end -- else -- begin -- set @page = @pageIndex-@page+1 --后半部分数据处理 -- if @page <= 1 --最后一页数据显示 -- set @strTmp=@SqlSelect+‘ * from (‘+@SqlSelect+‘ top ‘+ CAST(@lastcount as VARCHAR(4))+‘ ‘+ @fldName+‘ from ‘+@tblName -- +‘ where (1=1) ‘+ @strCondition +‘ order by ‘+ @fldSort +‘ ‘+ @strSortType+‘) AS TempTB‘+‘ order by ‘+ @fldSort +‘ ‘+ @strFSortType -- else if(@Sort=1) -- set @strTmp=@SqlSelect+‘ * from (‘+@SqlSelect+‘ top ‘+ CAST(@pageSize as VARCHAR(4))+‘ ‘+ @fldName+‘ from ‘+@tblName -- +‘ where ‘+@ID+‘ >(select max(‘+ @ID +‘) from(‘+ @SqlSelect+‘ top ‘+ CAST(@pageSize*(@page-2)+@lastcount as Varchar(20)) +‘ ‘+ @ID +‘ from ‘+@tblName -- +‘ where (1=1) ‘+ @strCondition +‘ order by ‘+ @fldSort +‘ ‘+ @strSortType+‘) AS TBMaxID)‘ -- +‘ ‘+ @strCondition+‘ order by ‘+ @fldSort +‘ ‘+ @strSortType+‘) AS TempTB‘+‘ order by ‘+ @fldSort +‘ ‘+ @strFSortType -- else -- set @strTmp=@SqlSelect+‘ * from (‘+@SqlSelect+‘ top ‘+ CAST(@pageSize as VARCHAR(4))+‘ ‘+ @fldName+‘ from ‘+@tblName -- +‘ where ‘+@ID+‘ <(select min(‘+ @ID +‘) from(‘+ @SqlSelect+‘ top ‘+ CAST(@pageSize*(@page-2)+@lastcount as Varchar(20)) +‘ ‘+ @ID +‘ from ‘+@tblName -- +‘ where (1=1) ‘+ @strCondition +‘ order by ‘+ @fldSort +‘ ‘+ @strSortType+‘) AS TBMaxID)‘ -- +‘ ‘+ @strCondition+‘ order by ‘+ @fldSort +‘ ‘+ @strSortType+‘) AS TempTB‘+‘ order by ‘+ @fldSort +‘ ‘+ @strFSortType -- end -- end --------返回查询结果----- --exec sp_executesql @strTmp --select datediff(ms,@timediff,getdate()) as 耗时 ----print @strTmp --SET NOCOUNT OFF --GO

数据库分页全集

标签:

原文地址:http://www.cnblogs.com/hy1991/p/4276674.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!