Paging with Oracle and sql server and generic paging method

前端 未结 7 1085
予麋鹿
予麋鹿 2020-12-17 00:29

I want to implement paging in a gridview or in an html table which I will fill using ajax. How should I write queries to support paging? For example if pagesize is 20 and wh

相关标签:
7条回答
  • There is no uniform way to ensure paging across various RDBMS products. Oracle gives you rownum which you can use in where clause like:

    where rownum < 1000
    

    SQL Server gives you row_id( ) function which can be used similar to Oracle's rownum. However, row_id( ) isn't available before SQL Server 2005.

    0 讨论(0)
  • 2020-12-17 01:10

    I believe that both have a ROWNUM analytic Function. Use that and you'll be identical.

    In Oracle it is here

    ROW_NUMBER

    Yep, just verified that ROW_NUMBER is the same function in both.

    0 讨论(0)
  • 2020-12-17 01:10

    "Because...data can be change from other sessions." What do you want to happen for this ?

    For example, user gets the 'latest' ten rows at 10:30.

    At 10:31, 3 new rows are added (so those ten being view by the user are no longer the latest).

    At 10:32, the user requests then 'next' ten entries.

    Do you want that new set to include those three that have been bumped from 8/9/10 down to 11/12/13 ? If not, in Oracle you can select the data as it was at 10:30

    SELECT * FROM table_1 as of timestamp (timestamp '2009-01-29 10:30:00'); 
    

    You still need the row_number logic, eg

     select * from
        (SELECT a.*, row_number() over (order by hire_date) rn
        FROM hr.employees as of timestamp (timestamp '2009-01-29 10:30:00') a)
     where rn between 10 and 19
    
    0 讨论(0)
  • 2020-12-17 01:15
    select * 
      from ( select /*+ FIRST_ROWS(n) */   a.*,
          ROWNUM rnum 
          from ( your_query_goes_here, 
          with order by ) a 
          where ROWNUM <= 
          :MAX_ROW_TO_FETCH ) 
    where rnum  >= :MIN_ROW_TO_FETCH;
    

    Step 1: your query with order by

    Step 2: select a.*, ROWNUM rnum from ()a where ROWNUM <=:MAX_ROW_TO_FETCH

    Step 3: select * from ( ) where rnum >= :MIN_ROW_TO_FETCH; put 1 in 2 and 2 in 3

    0 讨论(0)
  • 2020-12-17 01:15

    If the expected data set is huge, I'd recommend to create a temp table, a view or a snapshot (materialized view) to store the query results + a row number retrieved either using ROWNUM or ROW_NUMBER analytic function. After that you can simply query this temp storage using row number ranges. Basically, you need to separate the actual data fetch from the paging.

    0 讨论(0)
  • 2020-12-17 01:26

    Unfortunately, the methods for restricting the range of rows returned by a query vary from one DBMS to another: Oracle uses ROWNUM (see ocdecio's answer), but ROWNUM won't work in SQL Server.

    Perhaps you can encapsulate these differences with a function that takes a given SQL statement and first and last row numbers and generates the appropriate paginatd SQL for the target DBMS - i.e. something like:

    sql = paginated ('select empno, ename from emp where job = ?', 101, 150)
    

    which would return

    'select * from (select v.*, ROWNUM rn from ('
     + theSql
     + ') v where rownum < 150) where rn >= 101'
    

    for Oracle and something else for SQL Server.

    However, note that the Oracle solution is adding a new column RN to the results that you'll need to deal with.

    0 讨论(0)
提交回复
热议问题