Parameterize FETCH FIRST n ROWS ONLY in DB2

2020-02-14 20:15发布

问题:

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) but didn't understand his approach using ':1'.

回答1:

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


回答2:

Try this, where V_NBR is your passed in parameter for the number of rows you want:

FETCH FIRST ' || DIGITS ( V_NBR ) || '  ROWS ONLY '