0

Consider the following.

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;
/

Don't try to understand what it does. It's just a stripped/vandalized version of the actual code. The juice though remains. What I want to do, is have two cursors. The second cursor is dynamic and it depends on the rows that the first cursor returns.

The above results in ORA-06511: PL/SQL: cursor already open .

1 Answer 1

2

The problem is that you have lines which read

open department_cursor;  
open person_cursor;

and later you have other lines which read

for department_row in department_cursor loop

and

for person_row in person_cursor loop

The later lines are attempting to open cursors which are already open.

I suggest you rewrite your code as:

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;

Share and enjoy.

Sign up to request clarification or add additional context in comments.

1 Comment

I didn't know that this syntax opens the cursor too. Thank you. It worked. I will mark it in too mins.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.