I am currently toying with a union query whose members contain a cursor, and I get an ORA-22902 error.
Here is the query :
select owner "obj_owner"
,table_name "obj_name"
,'TABLE' "obj_type"
,cursor(select owner
from all_synonyms
where table_owner like all_tables.owner
and table_name like all_tables.table_name
) "syn_owner"
from all_tables
where owner in ('USERA','USERB','USERC')
union
select owner "obj_owner"
,object_name "obj_name"
,'PACKAGE' "obj_type"
,cursor(select owner
from all_synonyms
where table_owner like all_objects.owner
and table_name like all_objects.object_name
) "syn_owner"
from all_objects
where owner in ('USERA','USERB','USERC')
and object_type like 'PACKAGE'
order by "obj_owner" asc, "obj_name" asc
;
And here is the detailed error message :
ORA-22902 CURSOR expression not allowed
Cause: CURSOR on a subquery is allowed only in the top-level SELECT list of a query.
Action: Remove the CURSOR expression and retry the operation.
Now, I could easily get similar results otherwise, but I am curious as to why this query is erroneous, and I would like to know if it is possible to edit it to avoid this error.