SQL Query to return N rows from dual

前端 未结 10 1837
盖世英雄少女心
盖世英雄少女心 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:24

    connect by is such a wonderful thing. It helps you generated multiple rows with a single set of data available in dual table. This can help you generate huge no of rows for your dummy data. For example

    insert into test select a.* from test1 a,(select * from dual connect by level <=100000) b;
    

    or you can do something like this

    Example 2 : You want to print square and cube of numbers from 1 to 10.

    SQL> select level "No", power(level,2) "Square", power(level,3) "Cube"  from dual     connect by level <= 10;
    
        No     Square       Cube
    ---------- ---------- ----------
         1          1          1
         2          4          8
         3          9         27
         4         16         64
         5         25        125
         6         36        216
         7         49        343
         8         64        512
         9         81        729
        10        100       1000
    

    Hence you can manipulate it in whatever form you want. This is how you can return multiple rows from dual table. References : http://www.oraclebin.com/2012/12/multipe-rows-from-dual-table.html

提交回复
热议问题