recursive query with select * raises ORA-01789

This is a minimized version of complex recursive query. The query works when columns in recursive member (second part of union all) of recursive CTE are listed explicitly:

with t (c,p) as (
  select 2,1 from dual
), rec (c,p) as (
  select c,p from t
  union all
  select t.c,t.p from rec join t on rec.c = t.p
)
select * from rec

I don't get why error ORA-01789: query block has incorrect number of result columns is raised when specified t.* instead.

with t (c,p) as (
  select 2,1 from dual
), rec (c,p) as (
  select c,p from t
  union all
  select t.* from rec join t on rec.c = t.p
)
select * from rec

Why t.* is not equivalent to t.c,t.p here? Could you please point me to documentation for any reasoning?

UPDATE: reproducible on 11g and 18 (dbfiddle).

Upvotes: 3

Views: 109

Answers (2)

I finally asked on AskTom forum and according to response from Oracle expert Connor McDonald, this behavior is in compliance with documentation, namely the sentence The number of column aliases following WITH query_name and the number of columns in the SELECT lists of the anchor and recursive query blocks must be the same which can be found in this paragraph.

The point is, the expansion of star expression is done after checking whether the numbers of columns are same. Hence one must list columns explicitly, shortening to star is not possible.

Upvotes: 2

EJ Egyed
EJ Egyed

Reputation: 6094

Seems like there could be some kind of bug to me. I modified the query slightly just to test various cases and am now able to reproduce an ORA-00600 error in my Oracle 19.6.0.0.0 database! Running the problematic query on apex.oracle.com or on livesql.oracle.com (which is running 19.8.0.0.0) also results in errors. Reporting it to Oracle now!

Upvotes: 1

Related Questions