Question

I am looking for a possibility to run an (Oracle) SQL without querying an actual table. Here I found a tip with the DUAL table. Thats pretty good. But I'm looking for a short solution to select MULTIPLE rows from "nowhere". Is that possible? Here is the shortest query I can think of, which does what I need:

Select 1, 'foo' FROM DUAL union 
Select 2, 'bar' FROM DUAL union  
Select 3, 'more stuff' FROM DUAL

But if I want to have more rows in my result, it gets pretty unhandy. Is there a shorter way? Something like

Select 1, 'foo'; 2, 'bar'; 3, 'more stuff' from dual 
or 
Select * from (1, 'foo') union (2, 'bar') union (3, 'more stuff') 

I know, that doesn't work, but do you have an idea? Is there an easy way to transpose a queries' result? Something like:

Select transposed (1, 'foo', 2, 'bar', 3, 'more stuff') from dual 

I want to keep the amount "overhead-characters" at the lowest.

Was it helpful?

Solution

You can use connect by level to generate more records - something like:

select level, decode(level, 1,'foo', 2,'bar', 3,'more stuff')
from dual connect by level <= 3

OTHER TIPS

In case there are multiple Columns - then this works as well -

select
  REGEXP_SUBSTR (jango,'^[^#]*') as f1,
  REGEXP_SUBSTR(REGEXP_REPLACE(jango,'^([^#]*#){1}', ''),'^[^#]*') as f2,
  REGEXP_REPLACE(jango,'^([^#]*#){2}', '') as f3
from
  (
  Select decode(level,
    1, 'foo#koo#joo',
    2, 'bar#loo#too' ,
    3, 'more stuff#doo#dingo') as jango
  from dual connect by level <= 20
  )

Here is how it works - The inner query is same as above I have appended multiple Columns using # - need to take care that it is not a part in the regex family else we need to escape it.

  Select decode(level,
    1, 'foo#koo#joo',
    2, 'bar#loo#too' ,
    3, 'more stuff#doo#dingo') as jango
  from dual connect by level <= 20

Gives the following -

             Jango
-------------------
foo#koo#joo
bar#loo#too
more stuff#doo#dingo

Now the following piece selects from the output column - 'jango', anything upto #

  REGEXP_SUBSTR (jango,'^[^#]*') as f1,

  O/p --> foo

for the Second column we remove the contents of the 1st column followed by #

  REGEXP_REPLACE(jango,'^([^#]*#){1}', '')

  we get --> koo#joo

Now the 1st step - get the first field.

for more fields {1} can be increased.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top