Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write a table literal in Oracle?

Tags:

oracle

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

select 'create' as col from dual;

This can be used to build 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 rows. I did it in this way:

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

But is there a more compact notation for this? I tried

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

but it does not work.

like image 527
ceving Avatar asked Jul 13 '12 11:07

ceving


People also ask

What is a literal in Oracle?

A literal is an explicit numeric, character, string, or Boolean value not represented by an identifier.

Is text a valid literal in Oracle?

Text literals have properties of both the CHAR and VARCHAR2 datatypes: Within expressions and conditions, Oracle treats text literals as though they have the datatype CHAR by comparing them using blank-padded comparison semantics. A text literal can have a maximum length of 4000 bytes.

How can I create table in Oracle?

In Oracle, CREATE TABLE statement is used to create a new table in the database. To create a table, you have to name that table and define its columns and datatype for each column. Syntax: CREATE TABLE table_name.

What are literal values in SQL?

A literal value is an alphanumerical, hexadecimal, or numeric constant. A string constant contains one or more characters of the character set enclosed in two single straight quotation marks (' ') or double straight quotation marks (” “).


2 Answers

You can use collection type and TABLE operator, for example (works in Oracle 10g):

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

COLUMN_VALUE
--------------------------------------------------------------------------------
abc
def
ghi
like image 187
Marcin Wroblewski Avatar answered Oct 03 '22 15:10

Marcin Wroblewski


A couple of ways to generate rows. You could use rownum against a table with a larger number of rows:

SELECT roWnum AS a
  FROM user_objects
  WHERE rownum <= 3

You could 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
like image 37
Glenn Avatar answered Oct 03 '22 15:10

Glenn