Reality is a perception. Perceptions are not always based on facts, and are strongly influenced by illusions. Inquisitiveness is hence indispensable

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:

Popular Posts

Labels

About Me

Well for a start, I dont' want to!. Yes I am reclusive, no I am not secretive; Candid? Yes; Aspergers? No :). My friends call me an enthusiast, my boss calls me purist, I call myself an explorer, to summarise; just an inquisitive child who didnt'learn to take things for granted. For the sake of living, I work as a S/W engineer. If you dont' know what it means, turn back right now.