数据库是 Oracle 10.2.0.1.0 - 64 位,在 Red Hat Enterprise Linux ES 第 4 版(Nahant 更新 8)上运行
在 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);
/
在本例中,它返回 2 行,其中 UNION 的每个部分各 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 真正令人疯狂的是上面的第二个例子是only损坏的情况。例如,如果我采用上面示例 2 中的查询,然后从联合两侧删除 doc_name 字段,一切都会突然恢复正常。或者,如果我删除 utl_raw.cast_to_varchar2 位或联合本身(并单独运行每个部分)。正是 UNION、AND 子句和函数调用的精确组合导致了问题。
有人建议这可能是错误 6038461,“带有 UNION 和快速 DUAL 子查询的 SQL 结果错误”,但我认为这不是一个很好的选择。
有人知道第二个查询是怎么回事吗?
PS 我应该补充一点,在 TOAD 中没有错误 - 查询运行良好......