How to write a table literal in Oracle?

A table with one column and one row can be created with:

select 'create' as col from dual;

This can be used to create table joins:

with
  a as (select 'create' as ac from dual),
  b as (select 'delete' as bc from dual)
select * from a left outer join b on (ac = bc);

Now I would like to have two lines. I did it as follows:

select 'create' as col from dual
union
select 'delete' as col from dual;

But are there more compact notation for this? I tried

select ('create', 'delete') as col from dual;

but that will not work.

+5
source share
2 answers

For example, you can use the collection type and the TABLE statement (works in Oracle 10g):

SQL> SELECT column_value FROM TABLE(SYS.ODCIVARCHAR2LIST('abc', 'def', 'ghi'));

COLUMN_VALUE
--------------------------------------------------------------------------------
abc
def
ghi
+9
source

Several ways to generate strings. You can use rownum for a table with a lot of rows:

SELECT roWnum AS a
  FROM user_objects
  WHERE rownum <= 3

You can use a hierarchical query:

SELECT level AS a
  FROM dual
  CONNECT BY LEVEL <= 3

EDIT: change int sequence to alpha sequence:

SELECT CHR( ASCII('a') + level - 1 )
  FROM dual
  CONNECT BY LEVEL <= 3
+2
source

All Articles