Solved

ORA-06511 PL/SQL: cursor already open

Posted on 2002-06-17
6
3,464 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
  • 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 47

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 100 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Truncate is a DDL Command where as Delete is a DML Command. Both will delete data from table, but what is the difference between these below statements truncate table <table_name> ?? delete from <table_name> ?? The first command cannot be …
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 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 video explains what a user managed backup is and shows how to take one, providing a couple of simple example scripts.

910 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now