Paging with Oracle and sql server and generic paging method

前端 未结 7 1088
予麋鹿
予麋鹿 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条回答
  •  时光取名叫无心
    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.

提交回复
热议问题