?
Solved

ORA-06511 PL/SQL: cursor already open

Posted on 2002-06-17
6
Medium Priority
?
3,715 Views
Last Modified: 2012-05-04
Hi all,

Why doesn't the following code return ORA-06511 as I would have expected?

Thanks.

CREATE TABLE blah (col1 NUMBER);

DECLARE
   PROCEDURE do_something IS
      CURSOR c IS
         SELECT *
           FROM blah;
     
      r c%ROWTYPE;
   BEGIN
      OPEN c;
      FETCH c INTO r;
      IF (r.col1 != 50)
      THEN
         RETURN;
      END IF;
   END do_something;
   
BEGIN
   EXECUTE IMMEDIATE 'TRUNCATE TABLE blah';
   FOR i IN 1..5
   LOOP
      INSERT INTO blah
      VALUES (i);
   END LOOP;
   COMMIT;
   FOR i IN 1..10
   LOOP
      do_something;
   END LOOP;
END;
/

DROP TABLE blah;
0
Comment
Question by:Kong
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 3

Expert Comment

by:mnicoras
ID: 7084685
Hi,

try this and probably it will tell you more:

CREATE TABLE blah (col1 NUMBER);

SET SERVEROUTPUT ON;

DECLARE
  PROCEDURE do_something IS
     CURSOR c IS
        SELECT *
          FROM blah;
     r c%ROWTYPE;
  BEGIN
-- the part I add
     if c%isopen then
       dbms_output.put_line('cursor is open? '||'true');
     else
       dbms_output.put_line('cursor is open? '||'false');
     end if;
-- the part I add
     OPEN c;
     FETCH c INTO r;
     IF (r.col1 != 50)
     THEN
        RETURN;
     END IF;
  END do_something;
BEGIN
  EXECUTE IMMEDIATE 'TRUNCATE TABLE blah';
  FOR i IN 1..5
  LOOP
     INSERT INTO blah
     VALUES (i);
  END LOOP;
  COMMIT;
  FOR i IN 1..10
  LOOP
     do_something;
  END LOOP;
END;
/

DROP TABLE blah;


enjoy,
Marius Nicoras
0
 
LVL 3

Expert Comment

by:mnicoras
ID: 7084687
Hi,

and now try this:

CREATE TABLE blah (col1 NUMBER);

SET SERVEROUTPUT ON;

DECLARE
  PROCEDURE do_something IS
     CURSOR c IS
        SELECT *
          FROM blah;
     r c%ROWTYPE;
  BEGIN
-- the part I add
     if c%isopen then
       dbms_output.put_line('cursor is open? '||'true');
     else
       dbms_output.put_line('cursor is open? '||'false');
     end if;
-- the part I add
     OPEN c;
     FETCH c INTO r;
-- the part I add more
     if c%isopen then
       dbms_output.put_line('cursor is open? '||'true');
     else
       dbms_output.put_line('cursor is open? '||'false');
     end if;
     OPEN c;
     FETCH c INTO r;
-- the part I add more
     IF (r.col1 != 50)
     THEN
        RETURN;
     END IF;
  END do_something;
BEGIN
  EXECUTE IMMEDIATE 'TRUNCATE TABLE blah';
  FOR i IN 1..5
  LOOP
     INSERT INTO blah
     VALUES (i);
  END LOOP;
  COMMIT;
  FOR i IN 1..10
  LOOP
     do_something;
  END LOOP;
END;
/

DROP TABLE blah;

enjoy more,
Marius Nicoras
0
 
LVL 2

Author Comment

by:Kong
ID: 7084760
Thanks Marius,

You're trying to Open, Fetch, Open within the same scope - which results in ORA-6511, that's expected.

I think I've just explained my own question 8-/ is it possible that we're talking about scope here: since the cursor is opened within a subprogram, when that subprogram exits the cursor goes out of scope/is dereferenced and hence garbage collected. The next time OPEN is called, it has been 'implicitly' closed, thus the exception is not raised.

What do you think?

Here's a supporting example, where the cursor is still within the scope of the subprogram, thus raising the exception:

CREATE TABLE blah (col1 NUMBER);

DECLARE
  CURSOR c IS
     SELECT *
       FROM blah;
       
  r c%ROWTYPE;      

  PROCEDURE do_something IS
  BEGIN
     OPEN c;
     FETCH c INTO r;
     IF (r.col1 != 50)
     THEN
        RETURN;
     END IF;
  END do_something;
 
BEGIN
  EXECUTE IMMEDIATE 'TRUNCATE TABLE blah';
  FOR i IN 1..5
  LOOP
     INSERT INTO blah
     VALUES (i);
  END LOOP;
  COMMIT;
  OPEN c;
  FETCH c INTO r;
  FOR i IN 1..10
  LOOP
     do_something;
  END LOOP;
END;
/

DROP TABLE blah;
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 48

Expert Comment

by:schwertner
ID: 7085892
Open the cursor with this the code:

IF c%ISOPEN
  CLOSE c;
END IF;
  OPEN c;
0
 
LVL 3

Accepted Solution

by:
mnicoras earned 400 total points
ID: 7089505
Hi,

you have figure out exactly the idea for opened cursors.

it's a different context for cursor when sub-program it's closed. IN your last example the cursor is within the same context of the sub-program's.

Good for you that you understood correctly the problem. "schwertner" suggestion is obvious! you should not forget it, but I think you already know that, otherwise you didn't come-up with this issue.

best regards,
Marius Nicoras
0
 
LVL 2

Author Comment

by:Kong
ID: 7091348
Well done guys, thanks for your help too schwertner.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I remember the day when someone asked me to create a user for an application developement. The user should be able to create views and materialized views and, so, I used the following syntax: (CODE) This way, I guessed, I would ensure that useā€¦
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
This video shows setup options and the basic steps and syntax for duplicating (cloning) a database from one instance to another. Examples are given for duplicating to the same machine and to different machines
Suggested Courses

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question