code_junkie

How to implement database engine independent paging?

Nov 10th, 2011
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.35 KB | None | 0 0
  1. Query q = ...;
  2. q.setFirstResult (0);
  3. q.setMaxResults (10);
  4.  
  5. N - upper bound
  6. M - lower bound
  7.  
  8. create #temp (Id int identity, originalId int)
  9.  
  10. insert into #temp(originalId)
  11. select top N KeyColumn from MyTable
  12. where ...
  13.  
  14. select MyTable.* from MyTable
  15. join #temp t on t.originalId = MyTable.KeyColumn
  16. where Id between M and M
  17. order by Id asc
  18.  
  19. drop #temp
Add Comment
Please, Sign In to add comment