SQL to generate a list of numbers from 1 to 100

后端 未结 11 1472
悲&欢浪女
悲&欢浪女 2020-11-27 04:07

Using the DUAL table, how can I get a list of numbers from 1 to 100?

相关标签:
11条回答
  • 2020-11-27 04:28

    If you want your integers to be bound between two integers (i.e. start with something other than 1), you can use something like this:

    with bnd as (select 4 lo, 9 hi from dual)
    select (select lo from bnd) - 1 + level r
    from dual
    connect by level <= (select hi-lo from bnd);
    

    It gives:

    4
    5
    6
    7
    8
    
    0 讨论(0)
  • 2020-11-27 04:31

    Do it the hard way. Use the awesome MODEL clause:

    SELECT V
    FROM DUAL
    MODEL DIMENSION BY (0 R)
          MEASURES (0 V)
          RULES ITERATE (100) (
            V[ITERATION_NUMBER] = ITERATION_NUMBER + 1
          )
    ORDER BY 1
    

    Proof: http://sqlfiddle.com/#!4/d41d8/20837

    0 讨论(0)
  • 2020-11-27 04:35

    Another interesting solution in ORACLE PL/SQL:

        SELECT LEVEL n
          FROM DUAL
    CONNECT BY LEVEL <= 100;
    
    0 讨论(0)
  • 2020-11-27 04:35

    You could use XMLTABLE:

    SELECT rownum
    FROM XMLTABLE('1 to 100');
    
    -- alternatively(useful for generating range i.e. 10-20)
    SELECT (COLUMN_VALUE).GETNUMBERVAL() AS NUM
    FROM XMLTABLE('1 to 100');
    

    DBFiddle Demo

    0 讨论(0)
  • 2020-11-27 04:37

    Using GROUP BY CUBE:

    SELECT ROWNUM
    FROM (SELECT 1 AS c FROM dual GROUP BY CUBE(1,1,1,1,1,1,1) ) sub
    WHERE ROWNUM <=100;
    

    Rextester Demo

    0 讨论(0)
提交回复
热议问题