Database is Oracle 10.2.0.1.0 - 64bit running on Red Hat Enterprise Linux ES release 4 (Nahant Update 8)
In SQL*Plus following code run perfectly:
var comment_id number
exec :comment_id := 3052753
select e.label as doc_name,
e.url,
i.item_id,
'multi' as form_type
from cr_items i, cr_extlinks e
where i.parent_id = :comment_id
and e.extlink_id = i.item_id
UNION
select null as doc_name,
utl_raw.cast_to_varchar2(DBMS_LOB.SUBSTR(r.content, 2000, 1)) as url,
r.item_id,
'single' as form_type
from cr_revisions r
where r.revision_id = content_item.get_latest_revision(:comment_id);
/
In this case, it returns 2 rows, 1 from each part of the UNION. If I change the call to content_item.get_latest_revision as follows, it breaks as follows:
var comment_id number
exec :comment_id := 3052753
select e.label as doc_name,
e.url,
i.item_id,
'multi' as form_type
from cr_items i, cr_extlinks e
where i.parent_id = :comment_id
and e.extlink_id = i.item_id
UNION
select null as doc_name,
utl_raw.cast_to_varchar2(DBMS_LOB.SUBSTR(r.content, 2000, 1)) as url,
r.item_id,
'single' as form_type
from cr_revisions r
where r.revision_id = ( select content_item.get_latest_revision(:comment_id)
from dual);
/
The error:
SQL> where r.revision_id = ( select content_item.get_latest_revision(:comment_id) from dual)
*
ERROR at line 14:
ORA-00904: : invalid identifier
Now, what's really crazy about this piece of SQL is that the 2nd example above is the only case that breaks. For example, if I take the query in example 2 above and just remove the doc_name field from both sides of the union everything suddenly works again. Or if I remove the utl_raw.cast_to_varchar2 bit, or the union itself (and run each part separately). It's just that precise combination of UNION, AND clauses and function call that breaks.
Somebody suggested that it might be bug 6038461, 'Wrong results from SQL with UNION and a fast DUAL subquery', but I don't think that's a good fit.
Anyone have a clue what's up with the 2nd query?
PS I should add that in TOAD there is no error - the queries run fine...