这是一个可能的Oracle错误还是我错过了什么?

时间:2012-06-22 16:21:37

标签: oracle oracle10g union sqlplus

数据库是Oracle 10.2.0.1.0 - 在Red Hat Enterprise Linux ES第4版(Nahant Update 8)上运行的64位

在SQL * Plus中,以下代码运行完美:

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);
/

在这种情况下,它从UNION的每个部分返回2行,1行。 如果我按如下方式将调用更改为content_item.get_latest_revision,则会中断,如下所示:

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);

/

错误:

SQL> where r.revision_id = ( select content_item.get_latest_revision(:comment_id) from dual)
                                               *
ERROR at line 14:
ORA-00904: : invalid identifier

现在,这篇SQL的真正疯狂之处在于上面的第二个例子是唯一的案例。例如,如果我在上面的示例2中进行查询,只是从联合的两侧删除doc_name字段,一切都会突然再次起作用。或者,如果我删除utl_raw.cast_to_varchar2位或联合本身(并单独运行每个部分)。只是UNION,AND子句和函数调用的精确组合打破了。

有人建议可能是错误6038461,“使用UNION的SQL和快速DUAL子查询导致错误的结果”,但我认为这不合适。

任何人都知道第二个问题是什么?

PS我应该在TOAD中添加它没有错误 - 查询运行良好......

2 个答案:

答案 0 :(得分:0)

不是AND/WHERE column = (SELECT column....)的忠实粉丝,从整体上来说,写AND/WHERE column IN (SELECT column...)会更好。但在您的情况下,它看起来不像子查询中可能存在多行或多列。怎么样 -

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);


/

答案 1 :(得分:0)

我认为它不起作用,因为你有一个空行; SQLPlus讨厌他们。