2014-09-24 49 views
0

請考慮以下事項。內循環中的動態遊標。多個遊標

declare 

v_name  person.name%TYPE; 
v_surname person.surname%TYPE; 

cursor department_cursor is 
    select * from department; 

cursor person_cursor is 
    select * from person 
    where nvl(name, '') = nvl(v_name, '');  

begin 
    open department_cursor; 
    open person_cursor; 
    for department_row in department_cursor loop 

    v_name := department_row.name; 
    v_surname := department_row.surname; 

    for person_row in person_cursor loop 
     DBMS_OUTPUT.PUT_LINE('I got here:'||person_row.p_id); 
    end loop; 

    end loop; 
    close person_cursor; 
    close department_cursor; 
end; 
/

不要試圖瞭解它的功能。這只是實際代碼的剝離/破壞版本。 雖然果汁仍然存在。我想要做的是有兩個遊標。第二個遊標是動態的,它取決於第一個遊標返回的行。

以上結果爲ORA-06511: PL/SQL: cursor already open

回答

2

的問題是,你有閱讀

open department_cursor; 
open person_cursor; 

線,以後你有哪些閱讀

for department_row in department_cursor loop 

for person_row in person_cursor loop 

後來的行正試圖打開其他線路已經打開的遊標。

我建議你重寫代碼爲:

declare 
    v_name  person.name%TYPE; 
    v_surname person.surname%TYPE; 

    cursor department_cursor is 
    select * from department; 

    cursor person_cursor is 
    select * from person 
    where nvl(name, '') = nvl(v_name, '');  
begin 
    for department_row in department_cursor loop 
    v_name := department_row.name; 
    v_surname := department_row.surname; 

    for person_row in person_cursor loop 
     DBMS_OUTPUT.PUT_LINE('I got here:'||person_row.p_id); 
    end loop; 
    end loop; 
end; 

分享和享受。

+0

我不知道這個語法也打開了光標。謝謝。有效。我會標記它太多分鐘。 – alkis 2014-09-24 14:26:54