cancel
Showing results for 
Search instead for 
Did you mean: 

sybase sql for paging

Former Member
6,349

We use TOP command and ORDER_BY to implement paging for our application with Sybase database: select TOP 100 id, name from tableA order by id The problem is when add join tables, we start to receive duplicate "id" and hence the total record is less than expected (because one row on the main table can match many rows in child table). Anybody has a solution for this?

My query:

select TOP 100 a.id, a.name, b.name, c.name
  from tableA a 
  left outer join tableB b on a.id = b.id 
  left outer join tableC c on a.id = c.id 
 where a.id < pass_in_id order by a.id desc

Accepted Solutions (0)

Answers (1)

Answers (1)

MCMartin
Participant

Probably not elegant, but seems to be what you are looking for:

select ... where a.id in (select top 100 a.id from a where a.id<pass_in_id oder by a.id desc)

Fill your select part into the "..."

Anyway based on your joins this can result in more than 100 rows...but all first 100 ids will be included

Former Member
0 Kudos

I use sybase ASE and it doesn't allow to use TOP in subquery

MCMartin
Participant
0 Kudos

In this case 1. change the forum, 2. migrate to SQLA it is the much cooler database system 😉

Former Member

option 2 sounds good. Does SQLA allow order by in subquery also?

MCMartin
Participant
0 Kudos

yes, I double checked

VolkerBarth
Contributor

FWIW, just two hints to think over ASE vs. SQL Anywhere: