Skip to content
Advertisement

Oracle FETCH FIRST 1 ROW with UNION ALL statement

I’m trying to create a UNION ALL statement in SQL using the FETCH FIRST 1 ROW however when I do that it gives me an error MISSING KEYWORD

Here’s what my SQL looks like:

Select * From tabl1 where Date = '04-MAR-2020' FETCH FIRST 1 ROW

UNION ALL

Select * From tabl1 where Date = '05-MAR-2020' FETCH FIRST 1 ROW

is something like this doable. I would like to keep this as a single statement rather than adding a SUBQUERY or anything of that nature.

Advertisement

Answer

Just put each part in parentheses. Works in 12.2, anyway:

( Select * From tabl1 where Date = '04-MAR-2020' FETCH FIRST 1 ROW )
UNION ALL
( Select * From tabl1 where Date = '05-MAR-2020' FETCH FIRST 1 ROW )

My actual test query, for anyone interested was this:

(select   object_name 
 from     user_objects 
 where    object_type = 'TABLE' 
 order by object_name 
 fetch first 1 row only)
UNION ALL
(select   object_name 
 from     user_objects 
 where    object_type = 'VIEW' 
 order by object_name 
 fetch first 1 row only);
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement