SQL Query to return N rows from dual

前端 未结 10 2021
盖世英雄少女心
盖世英雄少女心 2021-02-13 10:31

I want to write a SQL query which accepts a bind variable (say :NUM) and its output consists of one column & :NUM number of rows, each row having its row number. i.e. if we

10条回答
  •  刺人心
    刺人心 (楼主)
    2021-02-13 11:07

    I didn't come up with this answer [ so make sure any votes go the right way!!] , it just my testing notes based on 'OMG Ponies' [who wasn't sure whether the method would work with binding variable] above for reference:

    Connected to:
    Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Production
    With the Partitioning, OLAP and Data Mining options
    
    SQL> var num_rows number
    SQL> begin select 20 into :num_rows from dual;
      2  end;
      3  /
    
    PL/SQL procedure successfully completed.
    
    SQL> select level from dual
      2  connect by level <=:num_rows;
    
         LEVEL
    ----------
             1
             2
             3
             4
     ...
    

提交回复
热议问题