Is this a possible Oracle bug or am I missing something?

无人久伴 提交于 2019-12-14 03:52:07

问题


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...


回答1:


Not a big fan of AND/WHERE column = (SELECT column....), holistically its better to write AND/WHERE column IN (SELECT column...). But in your case it does not look like there are possibility of multiple rows or columns in the sub query. How about-

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 IN ( select content_item.get_latest_revision(:comment_id) 
                          from dual);

/

OR

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 EXISTS (select 'x'
                   from dual
                    where content_item.get_latest_revision(:comment_id) =r.revision_id);


/



回答2:


I think it doesn't work because you have an empty line; SQLPlus hate them.



来源:https://stackoverflow.com/questions/11160230/is-this-a-possible-oracle-bug-or-am-i-missing-something

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!