1

我有一个表,其中包含表示层次结构的数据。从该表中获取单个“对象”的数据的最简单方法是递归查询。同一个表还存储与“对象”关联的“成员变量”。我认为在单个查询中查看对象结构以及关联的成员变量会很好,所以我尝试了类似的方法:

cursor object_explorer is
           select (level*2) lvl, ob.object_id, lpad(ot1.object_type_name, 2*level + length(ot1.object_type_name), '.') ob_typ_nam
              from obj_tab ob, obj_type ot1
                  , cursor (select lpad(mv.member_var_name, level + length(mv.member_var_name), ' ') var_nam, /*other stuff*/
                             from obj_type ot2, object_memberVar_value omv, member_variable mv
                              where mv.member_variable_id = omv.member_variable_id
                              and ot2.object_type_id = omv.object_type_id
                              and omv.object_id = ob.object_id)
              where ot1.object_type_id = ob.object_type_id
              and /*other filtering conditions unrelated to problem at hand*/
              start with ob.objecT_id = '1234567980ABC' 
              connect by nocycle ob.parent_object = prior ob.object_id;

...并且 Oracle 告诉我“不允许使用光标表达式”。

如果我将其作为两个单独的游标(循环遍历一个游标的结果,然后根据这些结果使用另一个游标),则一切正常,因此我不需要单游标解决方案。

我只是想知道为什么我不能使用游标表达式组合这两个查询 - 或者我可以组合它们而我只是以某种方式错过了它?

(Oracle版本为10g)

4

1 回答 1

2

我认为您不需要在那里使用 CURSOR 关键字。作为对ora-22902状态的解释,CURSOR() 仅适用于 SELECT 语句的投影。

我们可以在 FROM 子句中使用内联视图。在你的情况下,看起来像:

....
from obj_tab ob, obj_type ot1
     , (select omv.object_id
               , lpad(mv.member_var_name, level + length(mv.member_var_name), ' ') var_nam
               , /*other stuff*/
         from obj_type ot2, object_memberVar_value omv, member_variable mv
         where mv.member_variable_id = omv.member_variable_id
         and ot2.object_type_id = omv.object_type_id
         ) iv
where iv.object_id = ob.object_id
and /*filtering conditions unrelated to problem at hand*/
....

您的 WHERE 子句不够好,因为您需要将内联视图连接到 OBJ_TYPE 和/或 OBJ_TAB 的东西。这就是为什么我omv.object_id进入子查询的投影:为外部查询的 WHERE 子句提供一个钩子。

于 2010-12-02T23:29:36.743 回答