Tuesday, January 11, 2011

Paginated fetch queries

Without much fuss,

For DB2:



select * from
(
select ROW_NUMBER() OVER () rowNum, t1.* from @tableName@ t1
where blah.. blah..
)
as t00,
(
select count(COL_1) as totalPageRecordCount from @tableName@ t2
where blah.. blah..
)
as t01
where rowNum between @start@ and @end@


For Oracle:



select min_t.*, count_t.* from
(
select rownum as row_num, max_t.* from
(
select t1.*
from @data_tablename@ t1
where blah.. blah..
order by t1.rowid -- order by rowid for consistent results
) max_t
where
rownum <= @end@
) min_t,
(
select count(*) as totalPageRecordCount
from @data_tablename@ t1
where blah.. blah..
) count_t
where
row_num >= @start@


Note that ordering by row_id ensures consistent data fetch results.

No comments:

Post a Comment