sql分页 完整版
		
		- -- ===================================================
 - -- auhtor : 数据库之家
 - -- modified date : 01/10/2011 13:44:15
 - -- description : 分页存储
 - -- ===================================================
 - -- ========= ======== SQL 2000========================
 - create proc GetTablePage
 - (
 - @tbname nvarchar(4000), --查询的对象名及多表连接语句
 - @feildcol nvarchar(4000), --查询的字段,多个字段用','隔开
 - @pagesize int, --每页显示的记录尺寸
 - @pageindex int, --显示的页码
 - @strwhere nvarchar(4000) = null, --查询的限制条件
 - @strorder nvarchar(4000) = null, --排序字段,多个字段用','隔开,注明升降序
 - @counts int output, --查询到的总的记录数
 - @pagecounts int output --按pagesize分页的总页数
 - )
 - as
 - declare @strsql nvarchar(4000) --显示查询结果的同台sql语句
 - declare @strtmp nvarchar(4000) --分页总数控制sql语句
 - declare @pageint int --每页显示的记录数
 - declare @bitflag bit --表自增列信息!
 - declare @occsql nvarchar(4000) --自增列及其他列值查询sql语句
 - declare @keycol nvarchar(4000) --自增列
 - if isnull(@feildcol,N'') = N''
 - set @feildcol = N'*' --查询字段为空默认为*
 - if isnull(@pagesize,0) < 1
 - set @pagesize = 10 --默认每页显示条记录(输入记录尺寸不大于时)
 - if isnull(@pageindex,0) < 1
 - set @pageindex = 1 --默认显示页码为(输入页码不大于时)
 - if isnull(@strwhere,N'') = N'' --初始化查询的限制条件
 - set @strwhere = N''
 - else
 - set @strwhere = N' where ' + @strwhere
 - if isnull(@strorder,N'') = N'' --初始化查询的排序方式
 - set @strorder = N''
 - else
 - set @strorder = N' order by ' + @strorder
 - --计算总记录数及分页总数
 - if @counts is null
 - begin
 - set @strtmp = N' select count(*) ttt1,(count(*) + '+ltrim(@pagesize)+N' - 1)/'+ltrim(@pagesize)
 - +N' ttt2 into tempcol from ' + @tbname + @strwhere
 - exec(@strtmp)
 - select @counts = ttt1 from tempcol
 - select @pagecounts = ttt2 from tempcol
 - end
 - --分页设置
 - if (@pageindex <= @pagecounts)
 - begin
 - set @occsql = N' select ' + @feildcol + N' into tempocctable from ' + @tbname + @strwhere + @strorder
 - exec(@occsql)
 - select @bitflag = objectproperty(object_id('tempocctable'),'tablehasidentity')
 - select name INTO #tempocc
 - from syscolumns where id=object_id( 'tempocctable') and name!=''
 - if (@bitflag = 0)
 - begin
 - set @feildcol = ''
 - update #tempocc
 - set @feildcol = @feildcol + ',' + name
 - set @feildcol = stuff(@feildcol,1,1,'')
 - set @strsql = N' select identity(int,1,1) as iid,' + @feildcol + N' into resultocctable from tempocctable '
 - end
 - else
 - begin
 - select @keycol = [name] from syscolumns where id = object_id('tempocctable')
 - and columnproperty(object_id('tempocctable'),name,'isidentity') = 1
 - set @feildcol = ''
 - update #tempocc
 - set @feildcol = @feildcol + ',' + name where name != @keycol
 - set @feildcol = stuff(@feildcol,1,1,'')
 - set @strsql = N' select identity(int,1,1) as iid,' + @keycol + N' + 0 as ' + @keycol + N',' + @feildcol
 - +N' into resultocctable from tempocctable '
 - end
 - drop table #tempocc
 - set @strsql = @strsql +N' select top ' + ltrim(@pagesize) + N' ' + @feildcol + N' from resultocctable where iid not in (select top '
 - + ltrim((@pageindex - 1)*@pagesize) + N' iid ' +N' from resultocctable)'
 - print @strsql
 - exec sp_executesql @strsql
 - exec('drop table tempocctable,resultocctable,tempcol ')
 - end
 - -- ========= ======== SQL 2005========================
 - create proc getpage
 - (
 - @tbname nvarchar(4000), --查询的对象名及多表连接语句
 - @feildcol nvarchar(4000), --查询的字段,多个字段用','隔开
 - @pagesize int, --每页显示的记录尺寸
 - @pageindex int, --显示的页码
 - @strwhere nvarchar(4000) = null, --查询的限制条件
 - @strorder nvarchar(4000) = null, --排序字段,多个字段用','隔开,注明升降序
 - @counts int output, --查询到的总的记录数
 - @pagecounts int output --按pagesize分页的总页数
 - )
 - as
 - declare @strsql nvarchar(4000) --显示查询结果的同台sql语句
 - declare @strtmp nvarchar(4000) --分页总数控制sql语句
 - declare @pageint int --每页显示的记录数
 - declare @occsql nvarchar(4000) --自增列及其他列值查询sql语句
 - if isnull(@feildcol,N'') = N''
 - set @feildcol = N'*' --查询字段为空默认为*
 - if isnull(@pagesize,0) < 1
 - set @pagesize = 10 --默认每页显示条记录(输入记录尺寸不大于时)
 - if isnull(@pageindex,0) < 1
 - set @pageindex = 1 --默认显示页码为(输入页码不大于时)
 - if isnull(@strwhere,N'') = N'' --初始化查询的限制条件
 - set @strwhere = N''
 - else
 - set @strwhere = N' where ' + @strwhere
 - if isnull(@strorder,N'') = N'' --初始化查询的排序方式
 - set @strorder = N''
 - else
 - set @strorder = N' order by ' + @strorder
 - --计算总记录数及分页总数
 - if @counts is null
 - begin
 - set @strtmp = N' select count(*) ttt1,(count(*) + '+ltrim(@pagesize)+N' - 1)/'+ltrim(@pagesize)
 - +N' ttt2 into tempcol from ' + @tbname + @strwhere
 - exec(@strtmp)
 - select @counts = ttt1 from tempcol
 - select @pagecounts = ttt2 from tempcol
 - end
 - --分页设置
 - if (@pageindex <= @pagecounts)
 - begin
 - set @occsql = N' select row_number() over (order by getdate()) as iid,' + @feildcol + N' into tempocctable from '
 - + @tbname + @strwhere + @strorder
 - exec(@occsql)
 - set @feildcol = stuff((select ',' + name from syscolumns where id = object_id('tempocctable') for xml path('')),1,1,'')
 - set @strsql = N' select top ' + ltrim(@pagesize) + N' ' + @feildcol + N' from tempocctable where iid not in (select top '
 - + ltrim((@pageindex - 1)*@pagesize) + N' iid ' +N' from tempocctable)'
 - print @strsql
 - exec sp_executesql @strsql
 - exec('drop table tempocctable,tempcol ')
 - end
 
如果你有更好的,发上来给大家秀一下吧!
数据库之家,分享快乐~
								
				
				============ 欢迎各位老板打赏~ ===========
			
		与本文相关的文章
- · 三种SQL分页法
 - · SqlSever2005千万条记录分页优化经验总结
 - · springboot集成duckdb
 - · MYSQL AES解密
 - · mysql创建用户并赋予权限
 - · docker定时任务Mysql脚本
 - · docker安装mysql8注意事项
 - · 定时备份docker中的mysql
 - · docker-compose通过容器名连接mysql
 - · unity3d mysql error: The given key was not present in the dictionary.
 - · mysql服务性能优化—my.cnf配置说明详解(不同内存)
 - · MySQL 批量修改数据表编码及字符集
 
