本文實例匯總了Oracle實現分頁查詢的SQL語法,整理給大家供大家參考之用,詳情如下:
1.無ORDER BY排序的寫法。(效率最高)
經過測試,此方法成本最低,只嵌套一層,速度最快!即使查詢的數據量再大,也幾乎不受影響,速度依然!
sql語句如下:
SELECT * FROM (Select ROWNUM AS ROWNO, T.* from k_task T where Flight_date between to_date('20060501', 'yyyymmdd') and to_date('20060731', 'yyyymmdd') AND ROWNUM <= 20) TABLE_ALIAS WHERE TABLE_ALIAS.ROWNO >= 10;
2.有ORDER BY排序的寫法。(效率最高)
經過測試,此方法隨著查詢范圍的擴大,速度也會越來越慢!
sql語句如下:
SELECT * FROM (SELECT TT.*, ROWNUM AS ROWNO FROM (Select t.* from k_task 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;
3.無ORDER BY排序的寫法。(建議使用方法1代替)
此方法隨著查詢數據量的擴張,速度會越來越慢!
sql語句如下:
SELECT * FROM (Select ROWNUM AS ROWNO, T.* from k_task T where Flight_date between to_date('20060501', 'yyyymmdd') and to_date('20060731', 'yyyymmdd')) TABLE_ALIAS WHERE TABLE_ALIAS.ROWNO <= 20 AND TABLE_ALIAS.ROWNO >= 10; TABLE_ALIAS.ROWNO between 10 and 100;
4.有ORDER BY排序的寫法.(建議使用方法2代替)
此方法隨著查詢范圍的擴大,速度也會越來越慢!
sql語句如下:
SELECT * FROM (SELECT TT.*, ROWNUM AS ROWNO FROM (Select * from k_task T where flight_date between to_date('20060501', 'yyyymmdd') and to_date('20060531', 'yyyymmdd') ORDER BY FACT_UP_TIME, flight_no) TT) TABLE_ALIAS where TABLE_ALIAS.rowno BETWEEN 10 AND 20;
5.另類語法。(有ORDER BY寫法)
該語法風格與傳統的SQL語法不同,不方便閱讀與理解,為規范與統一標准,不推薦使用。此處貼出代碼供大家參考之用。
sql語句如下:
With partdata as( SELECT ROWNUM AS ROWNO, TT.* FROM (Select * from k_task 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) Select * from partdata where rowno >= 10;
6.另類語法 。(無ORDER BY寫法)
With partdata as( Select ROWNUM AS ROWNO, T.* From K_task T where Flight_date between to_date('20060501', 'yyyymmdd') and To_date('20060531', 'yyyymmdd') AND ROWNUM <= 20) Select * from partdata where Rowno >= 10;
相信本文所述代碼能夠對大家有一定的參考借鑒價值。
下面是用ORACLE數據庫pl/sql編程實現的一個方式:
-------------------創建一個包--------------------------
create or replace package pages_query_pak as
type pages_cursor is ref cursor; --定一個游標,保存數據查詢得到的結果集
end pages_query_pak;
------------------創建一個過程----------------------
create or replace procedure pages_pro(
tableName in varchar2,
pageSize in number,--每一頁顯示的記錄數
pageNow in number,--顯示第幾頁
myRows out number,--總記錄數
pageCount out number,--總頁數
page_cursor out pages_query_pak.pages_cursor--返回的記錄集 這裡有用了上面那個包
) is
v_sql varchar2(1000);
v_begin number:=(pageNow-1)*pageSize+1;
v_end number:=pageNow*pageSize;
begin
v_sql:='select * from (select bt.*,rownum rnum from (select * from '|| tableName ||')bt
where rownum<='||v_end||') where rnum>='||v_begin;
open page_cursor for v_sql;
--計算myRows和pageCount
v_sql:='select count(*) from '||tableName;
execute immediate v_sql into myRows;
if mod(myRows,pageSize)=0 then
pageCount:=myRows/pageSize;
else
pageCount:=myRows/pageSize+1;
end if;
end;
select top "+(end-begin)+",
numbers,
state,
title,
deptname,
time,
fromuser
from
(select oa_alarm.alarm_number numbers,oa_alarm.alarm_title title,oa_alarm.alarm_time time,oa_user.user_realname fromuser,oa_alarm.alarm_state state,oa_dept.dept_name deptname from
oa_alarm,oa_user,oa_dept where oa_alarm.alarm_fromuser=
oa_user.user_number and oa_alarm.alarm_dept=oa_dept.dept_number
deptsql order by oa_alarm.alarm_time desc)
where numbers not in (select top "+begin+",numbers from (select oa_alarm.alarm_number numbers from
oa_alarm,oa_user,oa_dept where oa_alarm.alarm_fromuser=
oa_user.user_number and oa_alarm.alarm_dept=oa_dept.dept_number
deptsql order by oa_alarm.alarm_time desc))
大致這樣的,試試可不可以