Parameterize FETCH FIRST n ROWS ONLY in DB2

2020-02-14 20:10发布

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'.

2条回答
再贱就再见
2楼-- · 2020-02-14 20:29

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

FETCH FIRST ' || DIGITS ( V_NBR ) || '  ROWS ONLY '  
查看更多
倾城 Initia
3楼-- · 2020-02-14 20:36

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
查看更多
登录 后发表回答