Home > Database > Mysql Tutorial > mssql server sql分页存储过程

mssql server sql分页存储过程

WBOY
Release: 2016-06-07 17:47:40
Original
1156 people have browsed it

一款很简单的sql分布代码,并且是一款通用sql哦 。

set quoted_identifier on
go
set ansi_nulls on
go


create proc execbypage

@sqlquery varchar(2000), --//输入参数:sql检索语句或表名
@pagesize int, --//输入参数:每页显示记录条数
@pageindex int --//输入参数:当前页码

as

set nocount on
set ansi_warnings off

declare @tmptablename varchar(50)
set @tmptablename = '##tb1516_' + replace(cast(newid() as varchar(40)),'-','') --//生成随机临时表名称

declare @subindex int
set @subindex = charindex('from',@sqlquery)
if (@subindex > 0)
begin --//带from的标准检索语句
declare @sqlquery1 varchar(2000)
declare @sqlquery2 varchar(2000)
set @sqlquery1 = substring(@sqlquery,1,@subindex - 1)
set @sqlquery2 = substring(@sqlquery,@subindex,len(@sqlquery))
set @sqlquery = @sqlquery1 + ',identity(numeric,1,1) as id1516 into ' + @tmptablename + ' ' + @sqlquery2
end
else --//不带from的表名
begin
set @sqlquery = 'select *,identity(numeric,1,1) as id1516 into ' + @tmptablename + ' from' + @sqlquery
end
exec(@sqlquery) --//建立并初始化临时表数据

declare @indexstart varchar(20),@indexend varchar(20)
set @indexstart = cast((@pageindex-1)*@pagesize+1 as varchar(20)) --//数据起始行id
set @indexend = cast(@pageindex * @pagesize as varchar(20)) --//数据结束行id

exec('select * from ' + @tmptablename + ' where id1516 between ' + @indexstart + ' and ' + @indexend) --//检索该页数据

exec('select max(id1516) as recordcount from ' + @tmptablename) --//提取总条数

exec('drop table ' + @tmptablename) --//删除临时表


go
set quoted_identifier off
go
set ansi_nulls on
go

1、确定存储的输入参数:
1)sql脚本,该参数接收完整的、正确的sql检索文本,可将原应用中写好的sql脚本直接传入
2)每页的数据容量,就是一页有多少条数据
3)当前页码
2、确定分页机制:
1)执行传入的sql脚本,并将结果生成临时表
2)修改临时表的结构,增加标识列字段
3)根据标识列字段,计算出指定页码内的记录范围,并返回
4)返回总数据条数,用于客户端进行分页显示

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template