2010-12-02 54 views
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)

+0

我不認爲你可以在from子句中使用Cursor表達式。我也不明白你在這裏試圖做什麼。你能舉一個你希望的輸出的例子嗎? – Craig 2010-12-02 21:57:32

+0

@克雷格:你說得對。我不得不將遊標表達式移動到SELECT子句。 – FrustratedWithFormsDesigner 2010-12-03 21:15:08

回答

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子句提供一個鉤子。