oracle做分页查询怎么做?
以前一直是用 mysql 的 limit
这次是用的 oracle 不会了 求救 展开
sql语句如下:
分页1
SELECT *
FROM (Select ROWNUM AS ROWNO, T.*
from 表名 T(别名)
where 表字段 between to_date('20060501', 'yyyymmdd') and to_date('20060731', 'yyyymmdd')
AND ROWNUM <= 20) TABLE_ALIAS
WHERE TABLE_ALIAS.ROWNO >= 10;
经过测试,此方法成本最低,只嵌套一层,速度最快,即使查询的数据量再大,也几乎不受影响,速度依然.
分页2:
SELECT *
FROM (SELECT TT.*, ROWNUM AS ROWNO
FROM (Select t.*
from 表名 T(别名)
where flight_date between to_date('20060501', 'yyyymmdd') and
to_date('20060531', 'yyyymmdd')
ORDER BY FACT_UP_TIME, flight_no) TT(别名二)
WHERE ROWNUM <= 20) TABLE_ALIAS
where TABLE_ALIAS.rowno >= 10;
经过测试,此方法随着查询范围的扩大,速度也会越来越慢,
其中,1和100是自己指定的查询范围。
综合案例预备
create or replace package type_pack
is
type cur_type is ref cursor;
end;
create or replace procedure all_data_pro(v_tableName varchar2,v_all_data out type_pack.cur_type,v_all_records out number)
is
v_sql varchar2(200);
v_sql1 varchar2(200);
begin
v_sql:='select * from '||v_tableName;
open v_all_data for v_sql;
v_sql1:='select count(*) from '||v_tableName;
execute immediate v_sql1 into v_all_records;--立即执行一条SQL语句
end;
语句块调用以上存储过程
declare
v_all_data type_pack.cur_type;
v_all_records number;
v_row_data emp%rowtype;
begin
all_data_pro('emp',v_all_data,v_all_records);
fetch v_all_data into v_row_data;
dbms_output.put_line(v_row_data.ename);
dbms_output.put_line(v_all_records);
end;
综合案例实现:
create or replace procedure page_access_pro
(v_tableName varchar2,v_page number,v_pagesize number,v_page_data out type_pack.cur_type,v_maxpage out number)
is
v_page_sql varchar2(200);
v_all_records_sql varchar2(100);
v_count_records number;
v_begin number;
v_end number;
begin
v_begin:=(v_page-1)*v_pagesize;
v_end:=v_page*v_pagesize;
v_page_sql:='select empno,ename,job,mgr,HIREDATE,SAL,COMM,DEPTNO from (select e.*, rownum rn from(select * from '||v_tableName||')e where rownum<='||v_end||') where rn>'||v_begin;
dbms_output.put_line(v_page_sql);
open v_page_data for v_page_sql;
v_all_records_sql:='select count(*) from '||v_tableName;
execute immediate v_all_records_sql into v_count_records; --立即执行
v_maxpage:=ceil(v_count_records/v_pagesize);
end;
测试调用(注意,抓取游标中数据,类型的问题)
declare
v_page_data type_pack.cur_type;
v_maxpage number;
v_data_row emp%rowtype;
begin
page_access_pro('emp',1,2,v_page_data,v_maxpage);
dbms_output.put_line('最大页数是:'||v_maxpage);
fetch v_page_data into v_data_row;
while v_page_data%found loop
dbms_output.put_line(v_data_row.ename);
fetch v_page_data into v_data_row;
end loop;
close v_page_data;
end;
from (select rownum r,t1.* from youtable t1 where rownum<?) t2
where t2.r>?
小于最大的,大于最小的,就是你需要的条数
如果你取40-50条之间的
只需要小于50 大于40就OK了.