views:

135

answers:

1

I'm trying to do the following:

  select * 
  from table      
  fetch first @param rows only

@param is an int.

DB2 would not have it. I've heard of concatenating it with ||, but I can't seem to get that to work.

Anyone have experience with this?

(PS I saw a similar question (http://stackoverflow.com/questions/2621420/db2-wont-allow-parameterize-fetch-first-x-rows-only) but didn't understand his approach using ':1'.

+1  A: 

You could try the following:

select t.*
from (select r.*, row_number() over() as row_num  
      from table r) as t
where row_num <= @param
Christian Maslen
@Christian I may try that... I just think there should be a way to pass in a parameter to the above query!
mint
I think (not 100% on this) the reason you can't with the above is the fetch first x rows only syntax is more of an optimization instruction than say a syntax feature for paging. It's instructing DB2 to not perform the usual aggressive prefetch reads thus saving some disk access.
Christian Maslen