2011-11-14 45 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的事实。 –