2011-11-14 74 views
0

我有一個返回一個ID的遊標函數。我需要使用第一個遊標的ID結果在另一個遊標中獲得一些字段。在另一個遊標中使用/調用遊標 - PL/Sql

所以我的第一個光標:

CREATE OR REPLACE function get_id(id number) 

CURSOR child_id 
    IS 
     SELECT table1_id 
      FROM table1,child 
       WHERE child_id = id 
      AND table1_id = child_chld_id; 

理想的情況下我的第二個光標應該是:

cursor grandchild_id 
is 
select table1_id from table1,child 
where child_id = (return value of id from cursor child_id) 
and table1_id = child_chld_id; 

我該怎麼辦呢?

+0

@Anne - 感謝編輯。我永遠無法想象如何做到這一點! – Cindy

+0

基本上:粘貼代碼,選擇代碼,點擊'{}'按鈕並確保上面的行是空的。希望有所幫助! – Anne

+0

爲什麼不把它們組合成一個查詢?它很可能會更有效率。 –

回答

1

光標可以接受參數:

cursor grandchild_id(other_child_id number) 
is 
select table1_id from table1,child 
where child_id = grandchild_id.other_child_id 
and table1_id = child_chld_id; 

每當你打開grandchild_id光標,只需通過適當的值作爲參數:

grandchild_id(the_value_you_obtained_from_the_first_cursor) 
+0

我將在Oracle發現者中使用它,它將運行數據庫中的所有ID ...在這種情況下它將如何工作?謝謝你的回覆 – Cindy

+0

@辛蒂:我不知道。您可能需要編輯您的問題,以包括您使用Discoverer的事實。 –