[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

ORA-06511 PL/SQL: cursor already open

Posted on 2002-06-17
6
Medium Priority
?
3,778 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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

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ā€¦
When it comes to protecting Oracle Database servers and systems, there are a ton of myths out there. Here are the most common.
This video shows how to Export data from an Oracle database using the Original Export Utility.  The corresponding Import utility, which works the same way is referenced, but not demonstrated.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

650 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