Search code examples
paginationsybase

Sybase offset for pagination


Is there any simple way to implement pagination in sybase? In postgres there are limit and offset in mysql there is limit X,Y. What about sybase? There is top clausure to limit results but to achieve full pagination there is also offset needed. It is not a problem if there are a few pags, I can simply trim results on the client side, but if there are millions of rows I would like to fetch only data that I need.


Solution

  • Quoting from http://www.isug.com/Sybase_FAQ/ASE/section6.2.html#6.2.12:

    Sybase does not have a direct equivalent to Oracle's rownum but its functionality can be emulated in a lot of cases.

    You can set a maximum rowcount, which will limit the number of rows returned by any particular query:

    set rowcount 150
    

    That limit will apply until it is reset:

    set rowcount 0
    

    You could select into a temporary table, then pull data from that:

    set rowcount 150
    
    select pseudo_key = identity(3),
           col1,
           col2
      into #tempA
      from masterTable
     where clause...
     order by 2,3
    
    select col1,col2 from #tempA where pseudo_key between 100 and 150
    

    You could optimize storage on the temp table by storing only ID columns, which you then join back to the original table for your select.

    The FAQ also suggests other solutions, including cursors or Sybperl.