-1

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.

shmail
  • 31
  • 1
  • 5

3 Answers3

2

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.

Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786
1

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;
Jim Macaulay
  • 4,709
  • 4
  • 28
  • 53
1

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