Create a table from DUAL

1.2k views Asked by At

How can I create the following table in Oracle.

+------+------+
| col1 | col2 |
+------+------+
| A    |    1 |
| B    |    2 |
+------+------+

I need it as an itermediate table in a WITH-clause.

3

There are 3 answers

0
Gordon Linoff On

You can use:

with t as (
      select 'A' as col1, 1 as col2 union all
      select 'B' as col1, 2 as col2
     )

You can then use t throughout the rest of the query.

0
Jim Macaulay On

Please use below query,

with tbl as 
  (
   select 'A' as col1, 1 as col2 from dual
   UNION
   select 'B' as col1, 2 as col2 from dual
)

select * from tbl;
0
Bob Jarvis - Слава Україні On

When you create a common table expression such as this you can name the columns in the CTE definition, which saves having them scattered through the SELECT statement:

WITH cteWork (COL1, COL2) AS
  (SELECT 'A', 1 FROM DUAL UNION ALL
   SELECT 'B', 2 FROM DUAL)
SELECT *
  FROM cteWork

db<>fiddle here