Home » SQL & PL/SQL » SQL & PL/SQL » cursor already open error
cursor already open error [message #524859] Tue, 27 September 2011 04:16 Go to next message
a_sanj
Messages: 12
Registered: September 2011
Junior Member
I am getting these errors..

ORA-06511: PL/SQL: cursor already open
ORA-06512: at "PCRM.PROC_EMP_SAL", line 10
ORA-06512: at "PCRM.PROC_EMP_SAL", line 17
ORA-06512: at line 1

can anyone help me in resolving it?
Re: cursor already open error [message #524861 is a reply to message #524859] Tue, 27 September 2011 04:18 Go to previous messageGo to next message
Littlefoot
Messages: 21808
Registered: June 2005
Location: Croatia, Europe
Senior Member
Account Moderator
Sure. Close the cursor first, then reopen it.

It might be easier to guess what might be wrong if you shared the code.
Re: cursor already open error [message #524862 is a reply to message #524859] Tue, 27 September 2011 04:18 Go to previous messageGo to next message
Michel Cadot
Messages: 68644
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator
Do not try to open a cursor that is already opened.

Regards
Michel
Re: cursor already open error [message #524864 is a reply to message #524862] Tue, 27 September 2011 04:20 Go to previous messageGo to next message
a_sanj
Messages: 12
Registered: September 2011
Junior Member

/* Formatted on 26-09-2011 11:20:21 (QP5 v5.115.810.9015) */
CREATE OR REPLACE PROCEDURE proc_emp_sal
IS
   v_comp_mnths      emp_new_sunita.comp_mnths%TYPE;
   v_compltd_mnths   emp_new_sunita.comp_mnths%TYPE;
   --num_total_rows    NUMBER;
   v_new_salary      emp_sunita.salary%TYPE;

   CURSOR emp_sal_cur
   IS
      SELECT empid, name, salary, deptno, joing_date
      FROM emp_sunita
      WHERE func_get_date (joing_date) >= 365;
BEGIN
   OPEN emp_sal_cur;


   FOR emp_rec IN emp_sal_cur
   LOOP
      BEGIN
         FETCH emp_sal_cur INTO emp_rec;

         v_compltd_mnths   := TRUNC (func_get_date  (emp_rec.joing_date) / 12);


         IF (func_get_date  (emp_rec.joing_date) >= 365
             AND func_get_date  (emp_rec.joing_date) <= 730)
         THEN
            BEGIN
               v_new_salary   := emp_rec.salary * 1.1;
               v_comp_mnths   := TRUNC (func_get_date  (emp_rec.joing_date) / 12);
               
               Insert into emp_new_sunita(comp_mnths,new_salary) values(V_COMP_MNTHS,V_NEW_SALARY);
            END;
         ELSIF (func_get_date  (emp_rec.joing_date) >= 731 AND func_get_date (emp_rec.joing_date) <= 1095)
         THEN
            BEGIN
               v_new_salary   := emp_rec.salary * 1.2;
               v_comp_mnths   := TRUNC (getdate (emp_rec.joing_date) / 12);
               Insert into emp_new_sunita(comp_mnths,new_salary) values(V_COMP_MNTHS,V_NEW_SALARY);
            END;
         ELSIF (getdate (emp_rec.joing_date) >= 1096)
         THEN
            BEGIN
               v_new_salary   := emp_rec.salary * 1.3;
               v_comp_mnths   := TRUNC (getdate (emp_rec.joing_date) / 12);
               Insert into emp_new_sunita(comp_mnths,new_salary) values(V_COMP_MNTHS,V_NEW_SALARY);
            END;
         END IF;
      END;
Exit when emp_sal_cur%NOTFOUND;

   END LOOP;

   CLOSE emp_sal_cur;
END;
/
Re: cursor already open error [message #524866 is a reply to message #524859] Tue, 27 September 2011 04:22 Go to previous messageGo to next message
Baranor
Messages: 83
Registered: September 2011
Location: Netherlands
Member


edit: ah, code statement came up. Easy pie:

You are using a cursor loop, ditch
OPEN emp_sal_cur;


and
CLOSE emp_sal_cur;


They are implicit.

(also see http://www.techonthenet.com/oracle/loops/cursor_for.php as additional info)

[Updated on: Tue, 27 September 2011 04:25]

Report message to a moderator

Re: cursor already open error [message #524867 is a reply to message #524866] Tue, 27 September 2011 04:24 Go to previous messageGo to next message
a_sanj
Messages: 12
Registered: September 2011
Junior Member
ok.thank you so much.i'll try to do that Smile
Re: cursor already open error [message #524870 is a reply to message #524867] Tue, 27 September 2011 04:30 Go to previous messageGo to next message
a_sanj
Messages: 12
Registered: September 2011
Junior Member
what statement i have to add to close it?
i have also applied exit condition.
Re: cursor already open error [message #524873 is a reply to message #524870] Tue, 27 September 2011 04:35 Go to previous messageGo to next message
Baranor
Messages: 83
Registered: September 2011
Location: Netherlands
Member
http://www.techonthenet.com/oracle/loops/cursor_for.php

See that link? Click on it, and do it exactly like that, no open or close statements. You are now opening the cursor, and then opening it again in the loop. Once you remove that first "open cursor", you will be closing it twice. Ditch (i.e. remove) the open and close statements, the loop does that for you (its implicitly added to the loop statement, you do not need to explicitly tell oracle to do that).
Re: cursor already open error [message #524875 is a reply to message #524870] Tue, 27 September 2011 04:38 Go to previous messageGo to next message
Littlefoot
Messages: 21808
Registered: June 2005
Location: Croatia, Europe
Senior Member
Account Moderator
Here's how you might have done it:
declare
  cursor emp_sal_cur is select ...
  cur_r emp_sal_cur%rowtype;
begin
  open emp_sal_cur;
  loop
    fetch emp_sal_cur into cur_r;
    exit when emp_sal_cur%notfound

    <do something>
  end loop;
  close emp_sal_cur;
end;


Or:
begin
  for cur_r in emp_sal_cur loop
  
    <do something>
  end loop;
end;


The way you used now is a mix of two options: first you opened a cursor with the OPEN statement, and then - once again - when entering the FOR loop. Pick the one you find the most useful, but let it be only one.

Obviously, if possible, a cursor FOR loop is easier to maintain - you don't have to open the cursor explicitly, you don't have to take care about exiting the loop, and you don't have to close it - Oracle does all those things for you.
Re: cursor already open error [message #524893 is a reply to message #524875] Tue, 27 September 2011 05:32 Go to previous message
a_sanj
Messages: 12
Registered: September 2011
Junior Member
Now i got it. i have made changes and now it is executing properly.thanks for your help.
Previous Topic: Fast Refresh Materialized Views
Next Topic: Howto view PL/SQL source code of Built-In Functions
Goto Forum:
  


Current Time: Wed Apr 24 07:22:11 CDT 2024