create
or
replace
package
Tools
is
type
ResultData
ref
cursor;
procedure
sp_Page(p_PageSize
int,
--每页记录数
p_PageNo
--当前页码,从
开始
p_SqlSelect
--查询语句,含排序部分
p_SqlCount
--获取记录总数的查询语句
p_OutRecordCount
out
int,--返回总记录数
p_OutCursor
ResultData);
end
Tools;
body
ResultData)
as
v_sql
v_count
int;
v_heiRownum
v_lowRownum
begin
----取记录总数
execute
immediate
into
v_count;
:=
----执行分页查询
*
p_PageSize;
-
p_PageSize
◆1;
'SELECT
FROM
(
SELECT
A.*,
rownum
rn
('||
||')
A
WHERE
=
'||
to_char(v_heiRownum)
||
'
)
B
to_char(v_lowRownum)
;
--注意对rownum别名的使用,第一次直接用rownum,第二次一定要用别名rn
OPEN
FOR
v_sql;
sp_Page;
我以前写过一个
PLSQL通用 分页 Function,可以参考一下我的博客:
调用的时候这样
declare
ocur tespackage.test_cursor;
v_count int:=0;
v_pagecount int :=0;
v_out int;
loop
fetch ocur into v_out ;
exit when ocur%notfound ;
dbms_output.put_line('count='||v_count);
end loop;
end ;
/
因为Oracle数据库没有Top关键字,所以这里就不能够像微软的数据据那样操作,这里有两种方法:
一种是利用相反的.
PAGESIZE:每页显示的记录数
CURRENTPAGE:当前页号
数据表的名字是:components
索引主键字是:id
select * from components where id not in(select id from components where rownum=(PAGESIZE*(CURRENTPAGE-1))) and rownum=PAGESIZE order by id;
如下例:
select * from components where id not in(select id from components where rownum=100) and rownum=10 order by id;
从101到记录开始选择,选择前面10条.
使用minus,即中文的意思就是减去,呵呵,这语句非常的有意思,也非常好记
如例:select * from components where rownum=10 minus select * from
一种是利用Oracle的rownum,这个是Oracle查询自动返回的序号,一般不显示,但是可以通过select rownum from [表名],可以看到,是从1到当前的记录总数.
select * from (select rownum tid,components.* from components where rownum=100) where tid=10;
select
from
(select
a.*,rownum
r
table_a)
a
where
rownum=b)
r=a
该sql语句实现了分页查询.
其中table_a表示你要查询的那张表,r=a,rownum=b中的a和b表示需要查询的记录的起止数.
需要做分页的话,上面的b可以改成currentPage*pageCount,a可以改成(currentPage-1)*pageCount,
currentPage表示当前页数,pageCount表示总页数
sql语句如下:
分页1
SELECT *
FROM (Select ROWNUM AS ROWNO, T.*
? from 表名 T(别名)
WHERE TABLE_ALIAS.ROWNO = 10;
经过测试,此方法成本最低,只嵌套一层,速度最快,即使查询的数据量再大,也几乎不受影响,速度依然.
FROM (SELECT TT.*, ROWNUM AS ROWNO
? FROM (Select t.*
? ? ? from 表名 T(别名)
? ? ? ORDER BY FACT_UP_TIME, flight_no) TT(别名二)
where TABLE_ALIAS.rowno = 10;
经过测试,此方法随着查询范围的扩大,速度也会越来越慢,
以上就是土嘎嘎小编为大家整理的oracle如何分页存储相关主题介绍,如果您觉得小编更新的文章只要能对粉丝们有用,就是我们最大的鼓励和动力,不要忘记讲本站分享给您身边的朋友哦!!