Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to have multiple rows with SELECT FROM DUAL?

Tags:

sql

oracle

Can I have something like

SELECT (1, 2, 4542, 342) FROM DUAL;

and get it like this?

  |  1    |
  |  2    |
  |  4542 |
  |  342  |
like image 555
Bobby Avatar asked May 07 '18 10:05

Bobby


People also ask

What does SELECT * from dual return?

When you select an expression say 1 from a table (can be dual or any other table), it will return the expression depending on the number of rows in the table. Eg:- Select 1 from dual; returns 1 as there is only one record in dual.

How can I get multiple row data in SQL?

Thus, we can use INSERT-SELECT-UNION query to insert data into multiple rows of the table. The SQL UNION query helps to select all the data that has been enclosed by the SELECT query through the INSERT statement.

What does SELECT * from dual mean in Oracle?

DUAL table has one column, DUMMY, defined to be VARCHAR2(1), and contains one row with a value X. Selecting from the DUAL table is useful for computing a constant expression with the SELECT statement. Because DUAL has only one row, the constant is returned only once.


3 Answers

Instead of DUAL, combine the TABLE operator with a pre-built collection to return multiple rows. This solution has a small syntax, avoids type conversions, and avoids potentially slow recursive queries. But it's good to understand the other solutions as well, they are all useful in different contexts.

select * from table(sys.odcinumberlist(1, 2, 4542, 342));
like image 143
Jon Heller Avatar answered Sep 19 '22 01:09

Jon Heller


Well if (1, 2, 4542, 342) were a string you could do this:

with cte as (
    SELECT '1, 2, 4542, 342' as str 
    FROM DUAL
)
select regexp_substr(str,'[^,]+',1,level) 
from cte
connect by level <= regexp_count(str, ',')+1
/
like image 43
APC Avatar answered Sep 20 '22 01:09

APC


No. dual has just one row, but you can use union all:

SELECT 1 FROM DUAL UNION ALL
SELECT 2 FROM DUAL UNION ALL
SELECT 4542 FROM DUAL UNION ALL
SELECT 342 FROM DUAL;

This is just one way to generate a table "on-the-fly" in Oracle.

like image 25
Gordon Linoff Avatar answered Sep 21 '22 01:09

Gordon Linoff