开发者

CREATE Oracle Procedure

开发者 https://www.devze.com 2023-02-01 15:47 出处:网络
I am trying to create a procedure and it created without error. However when I try to run it then I will get following error. Please advise

I am trying to create a procedure and it created without error. However when I try to run it then I will get following error. Please advise

SQL> begin
  2   Update_STUD_Fin ( '1011');
  3  end;
  4  /
begin
*
ERROR at line 1:
ORA-06511: PL/SQL: cursor already open
ORA-06512: at "ORAIN.UPDATE_STUD_FIN", line 3
ORA-06512: at "ORAIN.UPDATE_STUD_FIN", line 8
ORA-06512: at line 2

The Procedure is

SQL> CREATE OR REPLACE PROCEDURE Update_STUD_Fin ( AIDY_CODE IN VARCHAR2 ) IS
  2    CURSOR PublicationC IS
  3      SELECT SGidm from SGB
  4       WHERE SGCODE_EFF ='201030';
  5  BEGIN
  6    OPEN PublicationC;
  7  
  8    FOR PublicationR IN PublicationC
  9    LOOP
 10      DBMS_OUTPUT.PUT_LINE( PublicationR.SGidm );
 11    END LOOP;
 12  
 13    close PublicationC;
 14开发者_如何学C   
 15  END;
 16  /

Procedure created.


You cannot explicity OPEN the cursor and also use it in an implicit FOR loop. You much choose either implicit (FOR loop) or explicit(OPEN/FETCH/CLOSE).


If you use a cursor with a FOR/IN/LOOP, you don't need to open it explictly. Just write:

SQL> CREATE OR REPLACE PROCEDURE Update_STUD_Fin ( AIDY_CODE IN VARCHAR2 ) IS
  2    CURSOR PublicationC IS
  3      SELECT SGidm from SGB
  4       WHERE SGCODE_EFF ='201030';
  5  BEGIN
  8    FOR PublicationR IN PublicationC
  9    LOOP
 10      DBMS_OUTPUT.PUT_LINE( PublicationR.SGidm );
 11    END LOOP;
 12  
 15  END;
 16  /


6512 Error message indicates the line number in PL-SQL code that the error resulted. Are you sure you don't have another message before ?


I don't know why you're getting an ORA-06512 message with no other error message above it. Are you sure you have pasted the entire error message?

ORA-06512 is only used when printing out stack traces when an exception is raised in PL/SQL code. Here's a complete example:

ERROR at line 1:
ORA-01001: invalid cursor
ORA-06512: at "USER.SOME_PROCEDURE", line 5
ORA-06512: at line 1

The real error here is the ORA-01001. The ORA-06512s merely state where the error occurred: on line 5 of USER.SOME_PROCEDURE, which was called by line 1 of an anonymous PL/SQL block.

Taking a look at your code, I can spot a couple of problems:

  • When using FOR ... IN some_cursor LOOP ... then you should not explicitly open and close the cursor. That will be done for you automatically by the FOR loop.

  • You should also not attempt to close a cursor that hasn't been opened. If you try to do this, you'll get an ORA-01001 'invalid cursor' error message. I'm guessing you've put this in to ensure that the cursor was closed before you opened it, but unfortunately you can't do that.

In short, you should delete all of the OPEN and CLOSE statements from your procedure and try again.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号