ORACLE SQL:Get all integers between two numbers

前端 未结 13 1239
说谎
说谎 2020-12-03 13:42

Is there any way to select the numbers (integers) that are included between two numbers with SQL in Oracle; I don\'t want to create PL/SQL procedure or function.

Fo

13条回答
  •  眼角桃花
    2020-12-03 13:59

    This is a late addition. But the solution seems to be more elegant and easier to use.

    It uses a pipelined function that has to be installed once:

    CREATE TYPE number_row_type AS OBJECT 
    (
      num NUMBER
    );
    
    CREATE TYPE number_set_type AS TABLE OF number_row_type;
    
    CREATE OR REPLACE FUNCTION number_range(p_start IN PLS_INTEGER, p_end IN PLS_INTEGER)
        RETURN number_set_type
        PIPELINED
    IS
        out_rec number_row_type := number_row_type(NULL);
    
    BEGIN
      FOR i IN p_start .. p_end LOOP
        out_rec.num := i;
        pipe row(out_rec);
      END LOOP;
    
    END number_range;
    /
    

    Then you can use it like this:

    select * from table(number_range(1, 10));
    
    NUM
    ---
      1
      2
      3
      4
      5
      6
      7
      8
      9
     10
    

    The solution is Oracle specific.

提交回复
热议问题