CREATE Oracle Procedure

好久不见. 提交于 2020-01-10 05:16:38

问题


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   
 15  END;
 16  /

Procedure created.

回答1:


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).




回答2:


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  /



回答3:


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 ?




回答4:


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.



来源:https://stackoverflow.com/questions/4564689/create-oracle-procedure

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!