OraQA

Oracle Question and Answer

  • Do you have a solution to a problem? Do you have an unanswered question? Login and share it with the Oracle community. More...

Oracle News


Entries RSS feed

Comments RSS feed

What are the different ways to generate rows?

January 26th, 2006 By Eddie Awad

Sometimes you need to generate rows while working on a SQL problem. I have come across three different ways to accomplish this:

HR@XE> variable num_of_rows number
HR@XE> exec :num_of_rows := 11

PL/SQL procedure successfully completed.

Using cube with dual:

HR@XE> select rownum
  2      from (
  3          select null from dual
  4          group by cube (1,2,3,4) -- Generates 2*2*2*2 rows
  5          )
  6  where rownum <= :num_of_rows
  7  /

    ROWNUM
----------
         1
         2
         3
         4
         5
         6
         7
         8
         9
        10
        11

11 rows selected.

Using connect by level with dual (9i and above):

HR@XE> select *
  2  from (
  3      select level recnum from dual
  4      connect by level <= :num_of_rows
  5  )
  6  /

    RECNUM
----------
         1
         2
         3
         4
         5
         6
         7
         8
         9
        10
        11

11 rows selected.

Using just any table you can select from having the right number of rows:

HR@XE> select rownum recnum
  2  from all_objects
  3  where rownum <= :num_of_rows
  4  /

    RECNUM
----------
         1
         2
         3
         4
         5
         6
         7
         8
         9
        10
        11

11 rows selected.

5 Responses to “What are the different ways to generate rows?”

  1. Claudiu Ariton Says:

    Another way to generate rows is to use pipelined function

    create or replace type t_number as table of number
    /

    create or replace function generate(v_inf number, v_sup number) return t_number
    pipelined
    as

    begin

    for i in v_inf..v_sup loop

    pipe row (i);

    end loop;

    return;

    end;
    /

    select * from table(cast(generate(1,11) as t_number))
    /

    Best regards,
    Claudiu Ariton

  2. stbu Says:

    Seen on asktom website, you may use this:

    create or replace type array
    as table of number;
    
    create or replace function
    vtable( n in number default null )
    return array
    PIPELINED
    as
    begin
       for i in 1 .. nvl(n,999999999)
       loop
           pipe row(i);
       end loop;
       return;
    end;
    /
    

    Sample usage:

    select add_months ( to_date( '01.01.2006', 'DD.MM.YYYY' ), column_value-1) dt
      from table( vtable(12) );
    
    DT
    ----------
    01.01.2006
    01.02.2006
    01.03.2006
    01.04.2006
    01.05.2006
    01.06.2006
    01.07.2006
    01.08.2006
    01.09.2006
    01.10.2006
    01.11.2006
    01.12.2006
    
  3. gamyers Says:

    10G, MODEL clause, just plug in the ITERATE :

    SELECT x from dual
    MODEL DIMENSION BY (1 AS z) MEASURES (1 x)
    RULES ITERATE (7) (x[ITERATION_NUMBER]=ITERATION_NUMBER+1);

  4. Karl Reitschuster Says:

    Igor,
    your example with model clause ist very impressing for me! Did not worke with 10G model clause yet
    karl

  5. Karl Reitschuster Says:

    Hi,
    The Date variant of igors model example :

    
    SQL>
    SQL> SELECT add_months(trunc(sysdate), - (x-1)) AS date_gen from dual
      2  MODEL DIMENSION BY (1 AS z) MEASURES (1 x)
      3  RULES ITERATE (10) (x[ITERATION_NUMBER]=ITERATION_NUMBER+1);
    
    DATE_GEN
    -----------
    23.01.2006
    23.02.2006
    23.12.2005
    23.11.2005
    23.10.2005
    23.09.2005
    23.08.2005
    23.07.2005
    23.06.2005
    23.05.2005
    
    10 rows selected
    
    SQL> 
    

    Greetings
    Karl

Leave a Reply

You must be logged in to post a comment.

RSS feed for comments on this question