Sybase分页操作存储过程
2008-04-16 10:57
363 查看
/*--sybase分页操作--*/
--方法1:
--*********************************************
--按起始记录条数与终结记录条数进行划分
--@sqlStr为普通查询语句,语句中不能包括表中已有
--的Identity类型字段,否则存储过程将不能正常工作
--例:语句可以为select * from TestTable
--*********************************************
if(exists(select * from sysobjects where type = 'P' and name = 'sp_SplitPage1'))
drop proc sp_SplitPage1
go
create procedure sp_SplitPage1
(
@sqlStr varchar(8000), --需要进行查询的数据集
@startRecNum varchar(20), --起始记录行
@endRecNum varchar(20) --结束记录行
)
as
begin
declare @randnum varchar(20)
set @randnum = substring(convert(varchar,rand()),3,10)
set @sqlStr = stuff(@sqlStr,charindex(' from ',@sqlStr),6,' into tempdb..mytemp' + @randnum + ' from ')
execute (@sqlStr)
set @sqlStr = 'alter table tempdb..mytemp' + @randnum + ' add keyId numeric(10) IDENTITY primary key'
execute (@sqlStr)
set @sqlStr = 'select * from tempdb..mytemp' + @randnum + ' where keyId >= ' + @startRecNum
+ ' and keyId <= ' + @endRecNum
execute (@sqlStr)
set @sqlStr = 'drop table tempdb..mytemp' + @randnum
execute (@sqlStr)
end
go
--测试用例
declare @curdate datetime
set @curdate = getdate()
use TestDB
exec sp_SplitPage1 'select ID,Name from TestTable','1','9'
select datediff(millisecond,@curdate,getdate())
go
--方法2:
--*********************************************
--给出当前页数与每页记录数进行分页
--@sqlStr为普通查询语句,语句中不能包括表中已有
--的Identity类型字段,否则存储过程将不能正常工作
--例:语句可以为select * from TestTable
--*********************************************
if(exists(select * from sysobjects where type = 'P' and name = 'sp_SplitPage2'))
drop proc sp_SplitPage2
go
create procedure sp_SplitPage2
(
@sqlStr varchar(8000), --需要进行查询的数据集
@curPage int, --当前页数
@pageNum int --每页记录数
)
as
begin
declare @startRecNum varchar(20)
declare @endRecNum varchar(20)
--set @sqlStr = 'select DevId,OrgId from Device'
set @startRecNum = convert(varchar,(@curPage - 1) * @pageNum + 1)
set @endRecNum = convert(varchar,(@curPage - 1) * @pageNum + @pageNum)
declare @randnum varchar(20)
set @randnum = substring(convert(varchar,rand()),3,10)
set @sqlStr = stuff(@sqlStr,charindex(' from ',@sqlStr),6,' into tempdb..mytemp' + @randnum + ' from ')
execute (@sqlStr)
set @sqlStr = 'alter table tempdb..mytemp' + @randnum + ' add keyId numeric(10) IDENTITY primary key'
execute (@sqlStr)
set @sqlStr = 'select * from tempdb..mytemp' + @randnum + ' where keyId >= ' + @startRecNum
+ ' and keyId <= ' + @endRecNum
execute (@sqlStr)
set @sqlStr = 'drop table tempdb..mytemp' + @randnum
execute (@sqlStr)
end
go
--测试用例
declare @curdate datetime
set @curdate = getdate()
use TestDB
exec sp_SplitPage2 'select ID,Name from TestTable',3,5
select datediff(millisecond,@curdate,getdate())
go
--方法1:
--*********************************************
--按起始记录条数与终结记录条数进行划分
--@sqlStr为普通查询语句,语句中不能包括表中已有
--的Identity类型字段,否则存储过程将不能正常工作
--例:语句可以为select * from TestTable
--*********************************************
if(exists(select * from sysobjects where type = 'P' and name = 'sp_SplitPage1'))
drop proc sp_SplitPage1
go
create procedure sp_SplitPage1
(
@sqlStr varchar(8000), --需要进行查询的数据集
@startRecNum varchar(20), --起始记录行
@endRecNum varchar(20) --结束记录行
)
as
begin
declare @randnum varchar(20)
set @randnum = substring(convert(varchar,rand()),3,10)
set @sqlStr = stuff(@sqlStr,charindex(' from ',@sqlStr),6,' into tempdb..mytemp' + @randnum + ' from ')
execute (@sqlStr)
set @sqlStr = 'alter table tempdb..mytemp' + @randnum + ' add keyId numeric(10) IDENTITY primary key'
execute (@sqlStr)
set @sqlStr = 'select * from tempdb..mytemp' + @randnum + ' where keyId >= ' + @startRecNum
+ ' and keyId <= ' + @endRecNum
execute (@sqlStr)
set @sqlStr = 'drop table tempdb..mytemp' + @randnum
execute (@sqlStr)
end
go
--测试用例
declare @curdate datetime
set @curdate = getdate()
use TestDB
exec sp_SplitPage1 'select ID,Name from TestTable','1','9'
select datediff(millisecond,@curdate,getdate())
go
--方法2:
--*********************************************
--给出当前页数与每页记录数进行分页
--@sqlStr为普通查询语句,语句中不能包括表中已有
--的Identity类型字段,否则存储过程将不能正常工作
--例:语句可以为select * from TestTable
--*********************************************
if(exists(select * from sysobjects where type = 'P' and name = 'sp_SplitPage2'))
drop proc sp_SplitPage2
go
create procedure sp_SplitPage2
(
@sqlStr varchar(8000), --需要进行查询的数据集
@curPage int, --当前页数
@pageNum int --每页记录数
)
as
begin
declare @startRecNum varchar(20)
declare @endRecNum varchar(20)
--set @sqlStr = 'select DevId,OrgId from Device'
set @startRecNum = convert(varchar,(@curPage - 1) * @pageNum + 1)
set @endRecNum = convert(varchar,(@curPage - 1) * @pageNum + @pageNum)
declare @randnum varchar(20)
set @randnum = substring(convert(varchar,rand()),3,10)
set @sqlStr = stuff(@sqlStr,charindex(' from ',@sqlStr),6,' into tempdb..mytemp' + @randnum + ' from ')
execute (@sqlStr)
set @sqlStr = 'alter table tempdb..mytemp' + @randnum + ' add keyId numeric(10) IDENTITY primary key'
execute (@sqlStr)
set @sqlStr = 'select * from tempdb..mytemp' + @randnum + ' where keyId >= ' + @startRecNum
+ ' and keyId <= ' + @endRecNum
execute (@sqlStr)
set @sqlStr = 'drop table tempdb..mytemp' + @randnum
execute (@sqlStr)
end
go
--测试用例
declare @curdate datetime
set @curdate = getdate()
use TestDB
exec sp_SplitPage2 'select ID,Name from TestTable',3,5
select datediff(millisecond,@curdate,getdate())
go
相关文章推荐
- C#操作数据库,分页、执行存储过程等 [一] - ADO.NET入门之中
- sybase分页存储过程的实现
- 数据库操作 sqlserver查询存储过程+分页
- asp.net 操作带输入输出参数的分页存储过程(二)
- 利用jdbc对数据表进行分页,操作存储过程(有参与无参,返回游标),函数等
- asp.net 操作带输入输出参数的分页存储过程(—)
- Sybase分页显示存储过程
- C#操作数据库,分页、执行存储过程等 [二] - ADO.NET入门之中
- Sybase分页存储过程实现
- 适合千万数据查询分页操作的一个通用存储过程
- 使用数据2分处理的通用分页存储过程 前半部分与后半部分数据访问时间相同,同等访问速度提高一倍
- 分页存储过程
- SQL Server 存储过程的分页方案比拼
- Oralce 写一个分页的存储过程
- Oracle分页存储过程及java的具体调用方法
- [百万级]通用存储过程.分页存储过程
- SQL Server 存储过程的分页
- 分页存储过程收集(非本人原创)
- 数据分页存储过程
- 存储过程分页