Sybase offset for pagination

前端 未结 7 1954
旧巷少年郎
旧巷少年郎 2020-12-06 14:14

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 li

7条回答
  •  有刺的猬
    2020-12-06 14:52

    Unfortunately Sybase does not provide the ability to set a start and offset limit. The best you can achieve is to use the SET ROWCOUNT to limit the number of records returned. If you have 1,000 records and want to page by 50 entries then something like this will bring back the first page...

    set rowcount 50
    select * from orders
    

    For the second page...

    set rowcount 100
    select * from orders
    

    ...and then you can choose not to display the first 50 from within your Java code. Obviously as you page forward you end up having to return larger and larger data sets. Your question about what to do with 1,000,000 records doesn't seem practical for a user interface that is paginated. No user searches on Google and then pages forward 1,000 times looking for stuff.

    What if I have a Natural Key?

    If you do have a relatively large data set and you can use a natural key on your data, this will help limit the records returned. For example if you have a list of contacts and have an interface that allows your users to select A to Z to page the people in the directory based on Surname then you can do something like...

    set rowcount 50
    select * from people 
    where upper(surname) like 'B%'
    

    When there are more than 50 people with a surname starting with 'B' you use the same approach as above to page forward...

    set rowcount 100
    select * from people 
    where upper(surname) like 'B%'
    

    ... and remove the first 50 in Java code.

    Following on from this example, maybe you can limit searches by date, or some other piece of data meaningful to your users requriements.

    Hope this helps!

提交回复
热议问题